Javaapache-commons-netFtp进行文件文件夹的上传及日志的输出

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Javaapache-commons-netFtp进行文件、文件夹的上传下载及日志的输出用到了apache的commons-net-3.0.1.jar和log4j-1.2.15.jar这连个jar包Java代码如下:[java]viewplaincopyprint?1.packagecom.bjut.edu.cn.ftp;2.3.importjava.io.BufferedInputStream;4.importjava.io.BufferedOutputStream;5.importjava.io.File;6.importjava.io.FileInputStream;7.importjava.io.FileNotFoundException;8.importjava.io.FileOutputStream;9.importjava.io.IOException;10.importjava.util.TimeZone;11.importorg.apache.commons.net.ftp.FTPClient;12.importorg.apache.commons.net.ftp.FTPClientConfig;13.importorg.apache.commons.net.ftp.FTPFile;14.importorg.apache.commons.net.ftp.FTPReply;15.16.importorg.apache.log4j.Logger;17.18.publicclassFtp{19.privateFTPClientftpClient;20.privateStringstrIp;21.privateintintPort;22.privateStringuser;23.privateStringpassword;24.25.privatestaticLoggerlogger=Logger.getLogger(Ftp.class.getName());26.27./**28.*Ftp构造函数29.*/30.publicFtp(StringstrIp,intintPort,Stringuser,StringPassword){31.this.strIp=strIp;32.this.intPort=intPort;33.this.user=user;34.this.password=Password;35.this.ftpClient=newFTPClient();36.}37./**38.*@return判断是否登入成功39.**/40.publicbooleanftpLogin(){41.booleanisLogin=false;42.FTPClientConfigftpClientConfig=newFTPClientConfig();43.ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());44.this.ftpClient.setControlEncoding(GBK);45.this.ftpClient.configure(ftpClientConfig);46.try{47.if(this.intPort0){48.this.ftpClient.connect(this.strIp,this.intPort);49.}else{50.this.ftpClient.connect(this.strIp);51.}52.//FTP服务器连接回答53.intreply=this.ftpClient.getReplyCode();54.if(!FTPReply.isPositiveCompletion(reply)){55.this.ftpClient.disconnect();56.logger.error(登录FTP服务失败!);57.returnisLogin;58.}59.this.ftpClient.login(this.user,this.password);60.//设置传输协议61.this.ftpClient.enterLocalPassiveMode();62.this.ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);63.logger.info(恭喜+this.user+成功登陆FTP服务器);64.isLogin=true;65.}catch(Exceptione){66.e.printStackTrace();67.logger.error(this.user+登录FTP服务失败!+e.getMessage());68.}69.this.ftpClient.setBufferSize(1024*2);70.this.ftpClient.setDataTimeout(30*1000);71.returnisLogin;72.}73.74./**75.*@退出关闭服务器链接76.**/77.publicvoidftpLogOut(){78.if(null!=this.ftpClient&&this.ftpClient.isConnected()){79.try{80.booleanreuslt=this.ftpClient.logout();//退出FTP服务器81.if(reuslt){82.logger.info(成功退出服务器);83.}84.}catch(IOExceptione){85.e.printStackTrace();86.logger.warn(退出FTP服务器异常!+e.getMessage());87.}finally{88.try{89.this.ftpClient.disconnect();//关闭FTP服务器的连接90.}catch(IOExceptione){91.e.printStackTrace();92.logger.warn(关闭FTP服务器的连接异常!);93.}94.}95.}96.}97.98./***99.*上传Ftp文件100.*@paramlocalFile当地文件101.*@paramromotUpLoadePath上传服务器路径-应该以/结束102.**/103.publicbooleanuploadFile(FilelocalFile,StringromotUpLoadePath){104.BufferedInputStreaminStream=null;105.booleansuccess=false;106.try{107.this.ftpClient.changeWorkingDirectory(romotUpLoadePath);//改变工作路径108.inStream=newBufferedInputStream(newFileInputStream(localFile));109.logger.info(localFile.getName()+开始上传.....);110.success=this.ftpClient.storeFile(localFile.getName(),inStream);111.if(success==true){112.logger.info(localFile.getName()+上传成功);113.returnsuccess;114.}115.}catch(FileNotFoundExceptione){116.e.printStackTrace();117.logger.error(localFile+未找到);118.}catch(IOExceptione){119.e.printStackTrace();120.}finally{121.if(inStream!=null){122.try{123.inStream.close();124.}catch(IOExceptione){125.e.printStackTrace();126.}127.}128.}129.returnsuccess;130.}131.132./***133.*下载文件134.*@paramremoteFileName待下载文件名称135.*@paramlocalDires下载到当地那个路径下136.*@paramremoteDownLoadPathremoteFileName所在的路径137.**/138.139.publicbooleandownloadFile(StringremoteFileName,StringlocalDires,140.StringremoteDownLoadPath){141.StringstrFilePath=localDires+remoteFileName;142.BufferedOutputStreamoutStream=null;143.booleansuccess=false;144.try{145.this.ftpClient.changeWorkingDirectory(remoteDownLoadPath);146.outStream=newBufferedOutputStream(newFileOutputStream(147.strFilePath));148.logger.info(remoteFileName+开始下载....);149.success=this.ftpClient.retrieveFile(remoteFileName,outStream);150.if(success==true){151.logger.info(remoteFileName+成功下载到+strFilePath);152.returnsuccess;153.}154.}catch(Exceptione){155.e.printStackTrace();156.logger.error(remoteFileName+下载失败);157.}finally{158.if(null!=outStream){159.try{160.outStream.flush();161.outStream.close();162.}catch(IOExceptione){163.e.printStackTrace();164.}165.}166.}167.if(success==false){168.logger.error(remoteFileName+下载失败!!!);169.}170.returnsuccess;171.}172.173./***174.*@上传文件夹175.*@paramlocalDirectory176.*当地文件夹177.*@paramremoteDirectoryPath178.*Ftp服务器路径以目录/结束179.**/180.publicbooleanuploadDirectory(StringlocalDirectory,181.StringremoteDirectoryPath){182.Filesrc=newFile(localDirectory);183.try{184.remoteDirectoryPath=remoteDirectoryPath+src.getName()+/;185.this.ftpClient.makeDirectory(remoteDirectoryPath);186.//ftpClient.listDirectories();187.}catch(IOExceptione){188.e.printStackTrace();189.logger.info(remoteDirectoryPath+目录创建失败);190.}191.File[]allFile=src.listFiles();192.for(intcurrentFile=0;currentFileall

1 / 7
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功