package pub.ftp; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import sun.net.TelnetInputStream; import sun.net.TelnetOutputStream; import sun.net.ftp.FtpClient; /***/ /***FTPClient端调用模块*作者:*版本:1.0*时间:2007.07.30*/ public class FTPClient ... {/***//***类的初始化,建立ftp的连接,用户登录,指定ftp的传输流*@paramhost*@paramport*@paramuser*@parampsw*///建立一条与指定主机、指定端口上的FTP服务器的连接privateFtpClientaftp=newFtpClient();;privateDataOutputStreamoutputs;privateTelnetOutputStreamouts;privateTelnetInputStreaminps;publicvoidFTPClass(Stringhost,intport,Stringuser,Stringpsw)...{try...{//注册到FTP服务器aftp.openServer(host);System.out.println("登陆....");aftp.login(user,psw);System.out.println("登陆FTP服务器成功!");aftp.binary();}catch(IOExceptione)...{System.out.println("连接FTP服务器失败!");e.printStackTrace();}}/***//***通过ftp上传文件到服务器上*@paramlocalFile本地所要上传的文件*@paramremoteFile传到服务器上的文件名称*/publicbooleanupFile(StringlocalFile,StringremoteFile)...{booleanresult=true;if(aftp!=null)...{System.out.println("正在上传文件"+localFile+",请等待....");try...{Filefile=newFile(localFile);outs=aftp.put(remoteFile);FileInputStreamin=newFileInputStream(file);byte[]bytes=newbyte[1024];intc;while((c=in.read(bytes))!=-1)...{outs.write(bytes,0,c);}outs.close();in.close();System.out.println("上传文件"+localFile+"成功!");System.out.println("上传文件所在目录:"+remoteFile+"");}catch(Exceptione)...{e.printStackTrace();System.out.println("上传文件"+localFile+"失败!");result=false;}}else...{result=false;}returnresult;}/***//***下载FTP服务器上的文件*@paramlocalFile本地文件名*@paramremoteFile远程服务器文件名*/publicbooleandownFile(StringremoteFile,StringlocalFile)...{booleanresult=true;if(aftp!=null)...{System.out.println("正在下载文件"+remoteFile+",请等待....");try...{Filefile=newFile(remoteFile);inps=aftp.get(remoteFile);aftp.cd("D;/jiadong");FileInputStreamin=newFileInputStream(file);//RandomAccessFilegetFile=newRandomAccessFile(file,"rw");//getFile.seek(0);FileOutputStreamos=newFileOutputStream(file);byte[]bytes=newbyte[1024];intc;while((c=in.read(bytes))!=-1)...{//.write(bytes,0,c);os.write(bytes,0,c);}inps.close();in.close();os.close();System.out.println("下载文件"+remoteFile+"成功!");System.out.println("上传文件所在目录:"+localFile+"");}catch(Exceptione)...{e.printStackTrace();System.out.println("下载文件"+remoteFile+"失败!");result=false;}}returnfalse;}/***//***断开ftp连接*@throwsIOException**/publicvoiddisconnect()throwsIOException...{aftp.closeServer();System.out.println("FTP服务器连接断开!");}//返回当前目录的所有文件及文件夹publicArrayListgetFileList()throwsIOException...{BufferedReaderdr=newBufferedReader(newInputStreamReader(aftp.list()));ArrayListal=newArrayList();Strings="";while((s=dr.readLine())!=null)...{al.add(s);}returnal;}publicstaticvoidmain(String[]args)throwsIOException...{FTPClientftpClient=newFTPClient();ftpClient.FTPClass("192.168.8.154",21,"jiadong","jiadong");//ftpClient.upFile("F:/music/想念.mp3","/jia/hehe.mp3");//ftpClient.upFile("d:/jiadong","D:/workspace/msgfield.xml");ftpClient.downFile("hehe.xml","/jia/ruru.xml");ftpClient.disconnect();}}