java ftp 复制文件命令_Java实现FTP文件与文件夹的上传和下载

1 packageftp;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 public classFTPTest_04 {19 privateFTPClient ftpClient;20 privateString strIp;21 private intintPort;22 privateString user;23 privateString password;24

25 private static Logger logger = Logger.getLogger(FTPTest_04.class.getName());26

27 /**28 * Ftp构造函数29 */

30 public FTPTest_04(String strIp, intintPort, String user, String Password) {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 public booleanftpLogin() {41 boolean isLogin = false;42 FTPClientConfig ftpClientConfig = newFTPClientConfig();43 ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());44 this.ftpClient.setControlEncoding("GBK");45 this.ftpClient.configure(ftpClientConfig);46 try{47 if (this.intPort > 0) {48 this.ftpClient.connect(this.strIp, this.intPort);49 }else{50 this.ftpClient.connect(this.strIp);51 }52 //FTP服务器连接回答

53 int reply = 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(Exception e) {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 public voidftpLogOut() {78 if (null != this.ftpClient && this.ftpClient.isConnected()) {79 try{80 boolean reuslt = this.ftpClient.logout();//退出FTP服务器

81 if(reuslt) {82 logger.info("成功退出服务器");83 }84 }catch(IOException e) {85 e.printStackTrace();86 logger.warn("退出FTP服务器异常!" +e.getMessage());87 }finally{88 try{89 this.ftpClient.disconnect();//关闭FTP服务器的连接

90 }catch(IOException e) {91 e.printStackTrace();92 logger.warn("关闭FTP服务器的连接异常!");93 }94 }95 }96 }97

98 /***99 * 上传Ftp文件100 *@paramlocalFile 当地文件101 *@paramromotUpLoadePath上传服务器路径 - 应该以/结束102 **/

103 public booleanuploadFile(File localFile, String romotUpLoadePath) {104 BufferedInputStream inStream = null;105 boolean success = false;106 try{107 this.ftpClient.changeWorkingDirectory(romotUpLoadePath);//改变工作路径

108 inStream = new BufferedInputStream(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(FileNotFoundException e) {116 e.printStackTrace();117 logger.error(localFile + "未找到");118 }catch(IOException e) {119 e.printStackTrace();120 }finally{121 if (inStream != null) {122 try{123 inStream.close();124 }catch(IOException e) {125 e.printStackTrace();126 }127 }128 }129 returnsuccess;130 }131

132 /***133 * 下载文件134 *@paramremoteFileName 待下载文件名称135 *@paramlocalDires 下载到当地那个路径下136 *@paramremoteDownLoadPath remoteFileName所在的路径137 **/

138

139 public booleandownloadFile(String remoteFileName, String localDires,140 String remoteDownLoadPath) {141 String strFilePath = localDires +remoteFileName;142 BufferedOutputStream outStream = null;143 boolean success = false;144 try{145 this.ftpClient.changeWorkingDirectory(remoteDownLoadPath);146 outStream = new BufferedOutputStream(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(Exception e) {155 e.printStackTrace();156 logger.error(remoteFileName + "下载失败");157 }finally{158 if (null !=outStream) {159 try{160 outStream.flush();161 outStream.close();162 }catch(IOException e) {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 public booleanuploadDirectory(String localDirectory,181 String remoteDirectoryPath) {182 File src = newFile(localDirectory);183 try{184 remoteDirectoryPath = remoteDirectoryPath + src.getName() + "/";185 boolean makeDirFlag = this.ftpClient.makeDirectory(remoteDirectoryPath);186 System.out.println("localDirectory : " +localDirectory);187 System.out.println("remoteDirectoryPath : " +remoteDirectoryPath);188 System.out.println("src.getName() : " +src.getName());189 System.out.println("remoteDirectoryPath : " +remoteDirectoryPath);190 System.out.println("makeDirFlag : " +makeDirFlag);191 //ftpClient.listDirectories();

192 }catch(IOException e) {193 e.printStackTrace();194 logger.info(remoteDirectoryPath + "目录创建失败");195 }196 File[] allFile =src.listFiles();197 for (int currentFile = 0;currentFile < allFile.length;currentFile++) {198 if (!allFile[currentFile].isDirectory()) {199 String srcName =allFile[currentFile].getPath().toString();200 uploadFile(newFile(srcName), remoteDirectoryPath);201 }202 }203 for (int currentFile = 0;currentFile < allFile.length;currentFile++) {204 if(allFile[currentFile].isDirectory()) {205 //递归

206 uploadDirectory(allFile[currentFile].getPath().toString(),207 remoteDirectoryPath);208 }209 }210 return true;211 }212

213 /***214 * @下载文件夹215 *@paramlocalDirectoryPath本地地址216 *@paramremoteDirectory 远程文件夹217 **/

218 public booleandownLoadDirectory(String localDirectoryPath,String remoteDirectory) {219 try{220 String fileName = newFile(remoteDirectory).getName();221 localDirectoryPath = localDirectoryPath + fileName + "//";222 newFile(localDirectoryPath).mkdirs();223 FTPFile[] allFile = this.ftpClient.listFiles(remoteDirectory);224 for (int currentFile = 0;currentFile < allFile.length;currentFile++) {225 if (!allFile[currentFile].isDirectory()) {226 downloadFile(allFile[currentFile].getName(),localDirectoryPath, remoteDirectory);227 }228 }229 for (int currentFile = 0;currentFile < allFile.length;currentFile++) {230 if(allFile[currentFile].isDirectory()) {231 String strremoteDirectoryPath = remoteDirectory + "/"+allFile[currentFile].getName();232 downLoadDirectory(localDirectoryPath,strremoteDirectoryPath);233 }234 }235 }catch(IOException e) {236 e.printStackTrace();237 logger.info("下载文件夹失败");238 return false;239 }240 return true;241 }242 //FtpClient的Set 和 Get 函数

243 publicFTPClient getFtpClient() {244 returnftpClient;245 }246 public voidsetFtpClient(FTPClient ftpClient) {247 this.ftpClient =ftpClient;248 }249

250 public static void main(String[] args) throwsIOException {251 FTPTest_04 ftp=new FTPTest_04("10.0.0.102",21,"admin","123456");252 ftp.ftpLogin();253 System.out.println("1");254 //上传文件夹

255 boolean uploadFlag = ftp.uploadDirectory("D:\\test02", "/"); //如果是admin/那么传的就是所有文件,如果只是/那么就是传文件夹

256 System.out.println("uploadFlag : " +uploadFlag);257 //下载文件夹

258 ftp.downLoadDirectory("d:\\tm", "/");259 ftp.ftpLogOut();260 }261 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值