【FTP】使用org.apache.commons.net.ftp.FTPClient 实现FTP的上传下载
2017-05-26 10:55
766 查看
在此之前,在项目中加上FTP的架包
View Code
1 package com.sxd.ftp; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.OutputStream; 9 10 import org.apache.commons.net.ftp.FTPClient; 11 import org.junit.Test; 12 13 14 15 public class FtpUtils { 16 17 /** 18 * 获取FTP连接 19 * @return 20 */ 21 public FTPClient getFTPClient() { 22 FtpConfig config = new FtpConfig(); 23 FTPClient ftpClient = new FTPClient(); 24 boolean result = true; 25 try { 26 //连接FTP服务器 27 ftpClient.connect(config.getFtpHost(), config.getFtpPort()); 28 //如果连接 29 if (ftpClient.isConnected()) { 30 //提供用户名/密码登录FTP服务器 31 boolean flag = ftpClient.login(config.getFtpUser(), config.getFtpPassword()); 32 //如果登录成功 33 if (flag) { 34 //设置编码类型为UTF-8 35 ftpClient.setControlEncoding("UTF-8"); 36 //设置文件类型为二进制文件类型 37 ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); 38 } else { 39 result = false; 40 } 41 } else { 42 result = false; 43 } 44 //成功连接并 登陆成功 返回连接 45 if (result) { 46 return ftpClient; 47 } else { 48 return null; 49 } 50 } catch (Exception e) { 51 e.printStackTrace(); 52 return null; 53 } 54 } 55 56 57 58 /** 59 * 60 * @param localPath本地路径 61 * @param fileName文件名 62 * @param newPath上传至FTP的新位置 63 * @return 64 */ 65 public boolean testUpload(String localPath,String fileName,String newPath) { 66 boolean result = true; 67 FileInputStream in = null; 68 FTPClient ftpClient = getFTPClient(); 69 if (null == ftpClient) { 70 System.out.println("FTP服务器未连接成功!!!"); 71 return false; 72 } 73 try { 74 //当前上传的文件 75 File file = new File(localPath+fileName); 76 in = new FileInputStream(file); 77 //上传至FTP服务器的新位置 78 ftpClient.changeWorkingDirectory(newPath); 79 //上传 80 ftpClient.storeFile(fileName, in); 81 System.out.println(ftpClient.printWorkingDirectory()); 82 return result; 83 } catch (IOException e) { 84 e.printStackTrace(); 85 return false; 86 } finally { 87 close(in, null, ftpClient); 88 } 89 } 90 91 /** 92 * FTP下载 93 * @param fileName 文件名 94 * @param localPath 95 * @param remotePath 96 * @return 97 */ 98 public boolean testDownload(String localPath,String fileName, String remotePath) { 99 boolean result = true; 100 FileOutputStream out = null; 101 FTPClient ftpClient = getFTPClient(); 102 if (null == ftpClient) { 103 System.out.println("FTP服务器未连接成功!!!"); 104 return false; 105 } 106 try { 107 //要写到本地的位置 108 File file = new File(localPath + fileName); 109 out = new FileOutputStream(file); 110 //文件存储在FTP的位置 111 ftpClient.changeWorkingDirectory(remotePath); 112 //下载文件 113 ftpClient.retrieveFile(fileName, out); 114 System.out.println(ftpClient.printWorkingDirectory()); 115 return result; 116 } catch (IOException e) { 117 e.printStackTrace(); 118 return false; 119 } finally { 120 close(null, out, ftpClient); 121 } 122 } 123 124 125 /** 126 * 关闭 输入流或输出流 127 * @param in 128 * @param out 129 * @param ftpClient 130 */ 131 public static void close(InputStream in, OutputStream out,FTPClient ftpClient) { 132 if (null != in) { 133 try { 134 in.close(); 135 } catch (IOException e) { 136 e.printStackTrace(); 137 System.out.println("输入流关闭失败"); 138 } 139 } 140 if (null != out) { 141 try { 142 out.close(); 143 } catch (IOException e) { 144 e.printStackTrace(); 145 System.out.println("输出流关闭失败"); 146 } 147 } 148 if (null != ftpClient) { 149 try { 150 ftpClient.logout(); 151 ftpClient.disconnect(); 152 } catch (IOException e) { 153 e.printStackTrace(); 154 System.out.println("Ftp服务关闭失败!"); 155 } 156 } 157 } 158 159 160 @Test 161 public void test(){ 162 FtpConfig config = new FtpConfig(); 163 //上传文件 164 // testUpload("D:/","new4.txt" , config.getFtpPath()); 165 //下载 166 testDownload("D:/develop/", "new4.txt", config.getFtpPath()); 167 168 169 170 } 171 }
View Code
相关文章推荐
- 【FTP】org.apache.commons.net.ftp.FTPClient实现复杂的上传下载,操作目录,处理编码
- FTP文件的上传和下载---org.apache.commons.net.ftp.FTPClient
- FTP文件的上传和下载---org.apache.commons.net.ftp.FTPClient
- org.apache.commons.net.ftp.FTPClient文件上传下载,中文乱码
- org.apache.commons.net.ftp.FTPClient----根据文件名,路径检索文件 FTP上传 下载 删除文件
- org.apache.commons.net.ftp.FTPClient上传下载压缩图片
- org.apache.commons.net.ftp.FTPClient上传、下载、修改文件等功能
- org.apache.commons.net.ftp.FTPClient上传文件大小改变的解决方法
- 利用Apache的commons-net实现FTP的文件读取、上传、下载、删除和移动功能
- org.apache.commons.net.ftp.FTPClient使用
- [Android中级]使用Commons-net-ftp来实现FTP上传、下载的功能
- 使用org.apache.commons.net.ftp包开发FTP客户端,实现进度汇报,实现断点续传,中文支持
- [Android中级]使用Commons-net-ftp来实现FTP上传、下载的功能
- org.apache.commons.net.ftp.FTPClient 下载文件提示Software caused connection abort: recv failed
- 使用org.apache.commons.net.ftp包开发FTP客户端,实现进度汇报,实现断点续传,中文支持
- 使用org.apache.commons.net.ftp包开发FTP客户端,实现进度汇报,实现断点续传,中文支持
- 使用commons-net实现FTP文件上传与下载工具类
- 使用org.apache.commons.net.ftp包开发FTP客户端,实现进度汇报,实现断点续
- 利用org.apache.commons.net.ftp实现ftp下载,解决中文文件名乱码问题
- 使用org.apache.commons.net.ftp包开发FTP客户端,实现进度汇报,实现断点续传,中文支持