还剩10页未读,继续阅读
本资源只提供10页预览,全部文档请下载后查看!喜欢就下载吧,查找使用更方便
文本内容:
java编写ftp下载工具需要用到java写一个ftp的工具,因为只有一点点java基础,但是由于好几年不用,几乎算是不会了,只好一点点来搞,还好能捡起来不过因为是在Linux下使用javac编译,不是在WIN下使用IDE来做这些事情,所以在运行和编译上又费了一些时间,不过正是因为这样对JAVA的一些编译、运行的知识又了解了一些对于ftp下载工具,代码如下复制代码代码如下import java.io.File;import java.io.FilelnputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.lOException;import java.io.InputStream;import java.io.OutputStream;import java.net.SocketException;import org.apache,commons,net.ftp.FTPCIient;import org.apache,commons,net.ftp.FTPReply;public classFtpClient{private Stringhost;private intport;private Stringusername;复制代码代码如下:import org.apache,commons.net.ftp.FTPCIient;这个包在apache的网站上直接下载就行,解压后找到对应的jar包,在编译的时候进行引用复制代码代码如下export FTPPATH=〃${路径}/xxx.jar〃javac-classpath$CLASSPATH:$FTPPATH FtpClient.java同样,在运行的时候也要指定classpath复制代码代码如下java-classpath$CLASSPATH:$FTPPATH FtpClient建议不要把$FTPPATH包含在CLASSPATH中,用什么包就引用什么环境变量就行了,没必要一股脑都添加进去,就像我们没必要import所有的包一样private Stringpassword;private booleanbinaryTransfer=true;private booleanpassiveMode=true;private Stringencoding=〃UTF-8〃;private intclientTimeout=3000;private booleanflag=true;private FTPCIientftpClient=null;public StringgetHost{return host;}public voidsetHostString host{this.host=host;public int getPort{return port;public voidsetPort intport{this,port=port;public StringgetUsername{return username;public voidsetUsernameString username{this,username=username;public StringgetPassword{return password;public voidsetPasswordString password{this,password=password;public booleanisBinaryTransfer{return binaryTransfer;public voidsetBinaryTransferboolean binaryTransfer{this.binaryTransfer=binaryTransfer;public booleanisPassiveMode{return passiveMode;}public voidsetPassiveModeboolean passiveMode{this.passiveMode=passiveMode;}public StringgetEncodingO{return encoding;public voidsetEncodingString encoding{this,encoding=encoding;}public intgetClientTimeout{return clientTimeout;public voidsetClientTimeoutint clientTimeout{this.clientTimeout=clientTimeout;public FtpClientStringHost{this.username=anonymous”;this,encoding=〃utf-8〃;this.binaryTransfer=true;this.binaryTransfer=true;this.port=21;this,host=Host;try{this.ftpClient=getFTPClient;}catch Exceptione{System,out.printin z,Create FTPCIienterror!z,;}private FTPCIientgetFTPClientthrows lOExceptionFTPCIientftpClient=new FTPCIient;ftpClient.setControlEncodingencoding;connectftpClient;if passiveMode{ftpClient.enterLocalPassiveMode;setFileTypeftpClient;try ftpClient.setSoTimeoutclientTimeout;}catch SocketExceptione{throw newlOException/zSet timeouterror.〃,e;return ftpClient;private voidsetFileTypeFTPClient ftpClient throws lOException{try{if binaryTransfer{ftpClient.setFileTypeFTPCIient.BINARY_FILE_TYPE;}else{ftpClient.setFileTypeFTPClient.ASCII_FILE_TYPE;}}catch lOExceptione{throw newlOExceptionCould notto setfile type.〃,e;}public booleanconnect FTPCIientftpClientthrows lOException{try{ftpClient.connect host,port;int reply=ftpClient.getReplyCode;if FTPReply.isPositiveCompletionreply{if ftpClient.login username,password{setFileTypeftpClient;return true;}else{this.ftpClient.disconnect;throw newlOException/ZFTP serverrefused connection./z;}catch lOExceptione{if this.ftpClient.isConnected{try{this.ftpClient.disconnect;}catch lOExceptionel{throw newlOExceptionCould notdisconnect fromserver.〃,e;}}throw newlOExceptionz,Could notconnect toserver.z,,e;return false;private voiddisconnect throws lOException{try{this.ftpClient.logout;}catch lOExceptione{System,out.printin logoutmay timeout!/z;if this.ftpClient.isConnected{this.ftpClient.disconnect;}public InputStreamgetStreamString serverFilethrowslOException{InputStreaminStream=null;try inStream=this.ftpClient.retrieveFileStreamserverFile;System,out.printin^inStream getover!”;return inStream;}catch lOExceptione{System,out.printin,zget streamexception;return null;}public booleanwriteStreamInputStream input,String localFilethrowslOException{FileOutputStream fout=new FileOutputStreamlocalFile;int ch=0;ifinput二二null{System,out.printin z,input isnull;return false;ch=input,read;whilech!=-1{font.writech;ch=input.read;System.out.printin writeover!,z;return flag;}catch lOExceptione{throw newlOException,/Couldn,tgetfile fromserver.z,,e;}public booleanisExistString remoteFilePaththrowslOException{try{File file=new FileremoteFilePath;String remotePath=remoteFilePath.substring0,remoteFilePath.indexOffile.getName-1;String[]listNames=this.ftpClient.listNamesremotePath;System,out.printInremoteFilePath;forint i=0;ilistNames.length;i++{System.out.printlnlistNames[i];if remoteFilePath.equalslistNa.mes[i]{flag=true;System,out.printinfile:〃+file・getName+〃existed;break;}else{flag=false;}catch lOExceptione{throw newlOException Z,FILE EXCEPT10N,z,e;return flag;//main fortestingpublic staticvoid mainString[]args throwslOException{String hostname=z/cp01-testing-ps
7130.cpOl.baidu.com,z;String serverFile=,,/home/work/check_disk.sh〃;String localFile=,,/home/work/workspace/project/dhc2-0/dhc/base/ftp/taskget”;FtpClient ftp=new FtpClienthostname;System.out.printlnftp.isExistserverFile;ftp.writeStreamftp.getStreamserverFile,localFile;ftp.disconnect;}这个工具是为了配合另外一个Hadoop工具做集群上传用的,所以里面的把input和ou tput流分开了,也是为了方便另外一个工具使用补充一点,如何在linux配置运行:如果这样的代码需要在linux下环境运行,首先要配置好响应的包,例如。
个人认证
优秀文档
获得点赞 0