请教各位大虾,通过JAVA实现FTP文件传输

fsb_12345 2003-05-21 09:45:00
请教各位大虾,通过JAVA实现FTP文件传输,该怎么做??拜托了!!
...全文
292 点赞 收藏 19
写回复
19 条回复
cheng715 2003年07月31日
厉害,我也学学
回复 点赞
心平至和 2003年07月31日
是太长了,不是在长了,把两个接在一起,就构成一个完成的类了。

这样调用。
CFtp Ftp=new CFtp();
boolean Re=Ftp.Connect("10.70.72.98",21);
Ftp.Login("guo","jianzhi");
//Ftp.SetCurDir("/Test");
//System.out.println(Ftp.GetCurDir());
Ftp.DownFile("c:\\W2Ksp3_CN.exe","/guo.exe");
String Dir[]=Ftp.ListFile("/");//to member every time to change a port,if you want good.
if(Dir!=null)
for (int x = 0; x < Dir.length; x++)
{
System.out.println(Dir[x]);
}
//Ftp.ReName("/W2Ksp3_CN.exe","/Win2KSP3.exe");
// Ftp.MkDir("/guo");
//Ftp.MkDir("/guo/jian");
//Ftp.RmDir("/guo/jian");
//Ftp.RmDir("/guo");
//Ftp.SendCommand("HELP");
//Ftp.SendCommand("SYST");
Ftp.DisConnect();

QQ 121639903
回复 点赞
心平至和 2003年07月31日
public boolean PutFile(String LocalFile,String RemFile,boolean Pasv)
{
File LFile=new File(LocalFile);
long Len=LFile.length();
FileInputStream ReadFile;
ServerSocket SvrSocket;
try {
ReadFile = new FileInputStream(LocalFile);
}
catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
return false;
}

if(!SendCommand("TYPE I"))
return false;
InetAddress Addr=ftpClient.getLocalAddress();
String strAddr=Addr.getHostAddress();
strAddr=strAddr.replace('.',',');
strAddr+=",0,100";
try {
SvrSocket = new ServerSocket(100);
}
catch (IOException ex1) {
System.out.println(ex1.getMessage());
return false;
}
if(!SendCommand("PORT "+strAddr))
return false;
if(!SendCommand("STOR "+RemFile))
return false;

Socket RecvSock;
DataOutputStream Writer;
try {
RecvSock = SvrSocket.accept();
Writer=new DataOutputStream(RecvSock.getOutputStream());
}
catch (IOException ex2) {
System.out.println(ex2.getMessage());
return false;
}

byte Buff[]=new byte[1024];
int Read;
int ReadLen=0;
while(true)
{
try {
Read = ReadFile.read(Buff);
Writer.write(Buff,0,Read);
Writer.flush();
ReadLen+=Read;
if(ReadLen==Len)
break;//put finish.
}
catch (IOException ex3) {
System.out.println(ex3.getMessage());
return false;
}
}
try {

RecvSock.close();
Writer.close();
}
catch (IOException ex4) {
}

if(!SendCommand(""))
return false;
System.out.println("down load the file finish!");
return true;
}

public boolean SetCurDir(String Dir)
{
return SendCommand("CWD "+Dir);
}

public String GetCurDir()
{
if(!SendCommand("PWD"))
return null;
else
{
int Pos=strRead.indexOf("\"");
int Pos2=strRead.indexOf("\"",Pos+1);
String Dir=strRead.substring(Pos+1,Pos2);
return Dir;
}
}

public boolean MkDir(String Dir)
{
if(SendCommand("MKD"+Dir))
return true;
else
return false;
}

public boolean RmDir(String Dir)
{
if(SendCommand("RMD"+Dir))
return true;
else
return false;

}

public boolean ReName(String Old,String New)
{
if(SendCommand("RNFR "+Old))
return SendCommand("RNTO "+New);
return false;
}

public boolean DelFile(String File)
{
return SendCommand("DELE "+File);
}
public String[] ListFile(String ListCmd)
{
String StrDir=new String();
FileOutputStream OutFile;
ServerSocket SvrSocket;
if(!SendCommand("TYPE A"))
return null;
InetAddress Addr=ftpClient.getLocalAddress();
String strAddr=Addr.getHostAddress();
try {
SvrSocket = new ServerSocket(0);
}
catch (IOException ex1) {
System.out.println(ex1.getMessage());
return null;
}
int RecvPort=SvrSocket.getLocalPort();
strAddr=strAddr.replace('.',',');
int T1=RecvPort/256;
int T2=RecvPort%256;
strAddr+=","+String.valueOf(T1)+","+String.valueOf(T2);

Socket RecvSock;
if(!SendCommand("PORT "+strAddr))
return null;
SendCommand("LIST "+ListCmd);
try {
RecvSock = SvrSocket.accept();
}
catch (IOException ex2) {
System.out.println(ex2.getMessage());
return null;
}
DataInputStream DataRead;
try {
DataRead = new DataInputStream(RecvSock.getInputStream());
}
catch (IOException ex3) {
System.out.println(ex3.getMessage());
return null;
}
byte Buff[]=new byte[1024];
int Read;
while(true)
{
try {
Read=DataRead.read(Buff);
if(Read<=0)
break;
StrDir+=new String(Buff,0,Read);

}
catch (IOException ex4) {
System.out.println(ex4.getMessage());
return null;
}
}
try
{
RecvSock.close();
SvrSocket.close();
}
catch (IOException ex)
{
}
if(!SendCommand("NOOP"))
return null;
return StrDir.split("\r\n");
}
}
和上面的接上,在长了,成不了。
一个FTP类,支持断点续传,如果有问题SendMail
guo_jianzhi@163.net提供技术支持。我自已写的类。
回复 点赞
心平至和 2003年07月31日
package myftp;
import java.io.*;
import java.net.*;
import java.util.*;
public class CFtp {
public Socket ftpClient;
public boolean bIsConnect;
public String LastError;
public OutputStreamWriter sockWrite;
public InputStreamReader sockRead;
public String strRead;
char[] ReadBuff=new char[1024];
int RetCode;
public CFtp()
{
}
public boolean Login(String UserName,String Password)
{
if(SendCommand(""))
if(SendCommand("USER "+UserName))
if(SendCommand("PASS "+Password))
return true;
return false;
}
public boolean Connect(String IP,int Port)
{

try
{
ftpClient = new Socket(IP, 21, true);
}
catch (IOException ex)
{
System.out.println(ex.getMessage());
return false;
}

try {
OutputStream os = ftpClient.getOutputStream();
sockWrite=new OutputStreamWriter(os);
InputStream is= ftpClient.getInputStream();
sockRead=new InputStreamReader(is);
}
catch (IOException ex1) {
}
return true;
}
public boolean CanResume()
{
if(SendCommand("REST 100"))
{
System.out.println("Ftp can Resume to Send!");
return true;
}
System.out.println("System can not Resume Send!");
return false;
}
public boolean DownFile(String LocalFile,String RemFile)
{
//this not user pasv mode to translate the data.
FileOutputStream OutFile;
ServerSocket SvrSocket;
if(!SendCommand("TYPE I"))
return false;
InetAddress Addr=ftpClient.getLocalAddress();
String strAddr=Addr.getHostAddress();
try {
SvrSocket = new ServerSocket(0);
}
catch (IOException ex1) {
System.out.println(ex1.getMessage());
return false;
}
int RecvPort=SvrSocket.getLocalPort();
strAddr=strAddr.replace('.',',');
int T1=RecvPort/256;
int T2=RecvPort%256;
strAddr+=","+String.valueOf(T1)+","+String.valueOf(T2);

File DescFile=new File(LocalFile);
long FileLen=0;
if(CanResume())
{
if(DescFile.exists())
FileLen=DescFile.length();
else
try
{
DescFile.deleteOnExit();//delete old the file,then create new.
DescFile.createNewFile();
}
catch (IOException ex6)
{
}
SendCommand("REST " + String.valueOf(FileLen));
}
try {
OutFile = new FileOutputStream(DescFile,true);
}
catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
return false;
}
if(!SendCommand("PORT "+strAddr))
return false;
if(!SendCommand("RETR "+RemFile))
return false;

Socket RecvSock;
try {
RecvSock = SvrSocket.accept();
}

catch (IOException ex2) {
System.out.println(ex2.getMessage());
return false;
}
DataInputStream DataRead;
try {
DataRead = new DataInputStream(RecvSock.getInputStream());
}
catch (IOException ex3) {
System.out.println(ex3.getMessage());
return false;
}
byte Buff[]=new byte[1024];
int Read;
while(true)
{
try {
Read=DataRead.read(Buff);
if(Read<=0)
break;

OutFile.write(Buff,0,Read);
OutFile.flush();
}
catch (IOException ex4) {
System.out.println(ex4.getMessage());
return false;
}
}
try {
OutFile.close();
}
catch (IOException ex5) {
}
if(!SendCommand(""))
return false;
System.out.println("down load the file finish!");
return true;
}
public void DisConnect()
{
try {
SendCommand("QUIT");
ftpClient.close();
}
catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
public boolean SendCommand(String Cmd)
{
if(!WriteStr(Cmd+"\r\n"))
return false;
if(!ReadStr())
return false;
if (RetCode>=400)
return false;
return true;
}

public boolean ReadStr()
{
int Len;
try {
Len=sockRead.read(ReadBuff);
}
catch (IOException ex) {
return false;
}
strRead=String.valueOf(ReadBuff,0,Len-2);//off the \r\n
RetCode=Integer.parseInt(strRead.substring(0,3));
System.out.println(strRead);
return strRead.length()>0;
}

public boolean WriteStr(String Write)
{
char[] Arr=Write.toCharArray();
try {
sockWrite.write(Arr);
sockWrite.flush();//user the flush,than you can do the next.! OK
System.out.print(Write);
}
catch (IOException ex) {
return false;
}
return true;
}

回复 点赞
zhx_232 2003年07月31日
masterz(MS MVP) 好羡慕哦,能不能给我一个星星?

你用的是jbuilder7吗,在jbuilder7里面有一个很完整的例子哦。用的是IP*work,都有一个软件在里面,很不错呢
回复 点赞
iheartthrob 2003年07月31日
打包!
回复 点赞
老马哥V 2003年07月31日
我靠 这么简单的问题

你先上google查看下先
回复 点赞
runer 2003年07月02日
直接用java socket参考ftp协议自己写也可以
回复 点赞
Pasp 2003年07月02日
http://www.jscape.com/articles/simple_ftp_using_java.html

给分吧!里面讲的很详细!
回复 点赞
ji_jian24 2003年07月02日
to Pasp
jscape的包要不要银子?
回复 点赞
anvie 2003年07月01日
又见五星
回复 点赞
cno 2003年07月01日
1)显示FTP服务器上的文件

void ftpList_actionPerformed(ActionEvent e) {
String server=serverEdit.getText();//输入的FTP服务器的IP地址
String user=userEdit.getText(); file://登录FTP服务器的用户名
String password=passwordEdit.getText();//登录FTP服务器的用户名的口令
String path=pathEdit.getText();//FTP服务器上的路径
try {
FtpClient ftpClient=new FtpClient();//创建FtpClient对象
ftpClient.openServer(server);//连接FTP服务器
ftpClient.login(user, password);//登录FTP服务器
if (path.length()!=0) ftpClient.cd(path);
TelnetInputStream is=ftpClient.list();
int c;
while ((c=is.read())!=-1) {
System.out.print((char) c);}
is.close();
ftpClient.closeServer();//退出FTP服务器
} catch (IOException ex) {;}
}

2)从FTP服务器上下传一个文件

void getButton_actionPerformed(ActionEvent e) {
String server=serverEdit.getText();
String user=userEdit.getText();
String password=passwordEdit.getText();
String path=pathEdit.getText();
String filename=filenameEdit.getText();
try {
FtpClient ftpClient=new FtpClient();
ftpClient.openServer(server);
ftpClient.login(user, password);
if (path.length()!=0) ftpClient.cd(path);
ftpClient.binary();
TelnetInputStream is=ftpClient.get(filename);
File file_out=new File(filename);
FileOutputStream os=new
FileOutputStream(file_out);
byte[] bytes=new byte[1024];
int c;
while ((c=is.read(bytes))!=-1) {
os.write(bytes,0,c);
}
is.close();
os.close();
ftpClient.closeServer();
} catch (IOException ex) {;}
}


3)向FTP服务器上上传一个文件
void putButton_actionPerformed(ActionEvent e) {
String server=serverEdit.getText();
String user=userEdit.getText();
String password=passwordEdit.getText();
String path=pathEdit.getText();
String filename=filenameEdit.getText();
try {
FtpClient ftpClient=new FtpClient();
ftpClient.openServer(server);
ftpClient.login(user, password);
if (path.length()!=0) ftpClient.cd(path);
ftpClient.binary();
TelnetOutputStream os=ftpClient.put(filename);
File file_in=new File(filename);
FileInputStream is=new FileInputStream(file_in);
byte[] bytes=new byte[1024];
int c;
while ((c=is.read(bytes))!=-1){
os.write(bytes,0,c);}
is.close();
os.close();
ftpClient.closeServer();
} catch (IOException ex) {;}
}
}


回复 点赞
limingxi007 2003年07月01日
好恐怖啊,masterz(MS MVP)
回复 点赞
ji_jian24 2003年06月28日
faint
楼上好多星啊!!!!!!!!!!!!
回复 点赞
masterz 2003年06月23日
http://www.javaworld.com/javaworld/jw-04-2003/jw-0404-ftp.html
Java FTP client libraries reviewed
回复 点赞
smilejiangjun 2003年05月22日
up
回复 点赞
fsb_12345 2003年05月21日
能把代码贴出来吗??拜托!!
回复 点赞
leshui 2003年05月21日
其实我觉得很上传的原理差不多
就是动态的把路径写进去
回复 点赞
fsb_12345 2003年05月21日
怎么每人啊
回复 点赞
发动态
发帖子
Java EE
创建于2007-09-28

3.7w+

社区成员

22.5w+

社区内容

J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区公告
暂无公告