FTPUtil工具类

  1 package com.xxx.common.util;
  2 
  3 import java.io.File;
  4 import java.io.FileOutputStream;
  5 import java.io.IOException;
  6 import java.io.InputStream;
  7 import java.io.OutputStream;
  8 import org.apache.commons.net.ftp.FTP;
  9 import org.apache.commons.net.ftp.FTPClient;
 10 import org.apache.commons.net.ftp.FTPFile;
 11 import org.apache.commons.net.ftp.FTPReply;
 12 
 13 /**
 14  * ftp上传下载工具类
 15  */
 16 public class FTPUtil {
 17 
 18     /**
 19      * Description: 向FTP服务器上传文件
 20      * 
 21      * @param host
 22      *            FTP服务器hostname
 23      * @param port
 24      *            FTP服务器端口
 25      * @param username
 26      *            FTP登录账号
 27      * @param password
 28      *            FTP登录密码
 29      * @param basePath
 30      *            FTP服务器基础目录
 31      * @param filePath
 32      *            FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath
 33      * @param filename
 34      *            上传到FTP服务器上的文件名
 35      * @param input
 36      *            输入流
 37      * @return 成功返回true,否则返回false
 38      * @throws Exception 
 39      */
 40     public static boolean uploadFile(String host, int port, String username, String password, String basePath,
 41             String filePath, String filename, InputStream input) throws Exception {
 42         boolean result = false;
 43         FTPClient ftp = new FTPClient();
 44         try {
 45             int reply;
 46             ftp.connect(host, port);// 连接FTP服务器
 47             // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
 48             ftp.login(username, password);// 登录
 49             reply = ftp.getReplyCode();
 50             if (!FTPReply.isPositiveCompletion(reply)) {
 51                 ftp.disconnect();
 52                 return result;
 53             }
 54             // 切换到上传目录
 55             if (!ftp.changeWorkingDirectory(basePath + filePath)) {
 56                 // 如果目录不存在创建目录
 57                 String[] dirs = filePath.split("/");
 58                 String tempPath = basePath;
 59                 for (String dir : dirs) {
 60                     if (null == dir || "".equals(dir))
 61                         continue;
 62                     tempPath += "/" + dir;
 63                     if (!ftp.changeWorkingDirectory(tempPath)) {
 64                         if (!ftp.makeDirectory(tempPath)) {
 65                             return result;
 66                         } else {
 67                             ftp.changeWorkingDirectory(tempPath);
 68                         }
 69                     }
 70                 }
 71             }
 72             // 设置上传文件的类型为二进制类型
 73             ftp.setFileType(FTP.BINARY_FILE_TYPE);
 74             // 上传文件
 75             if (!ftp.storeFile(filename, input)) {
 76                 return result;
 77             }
 78             input.close();
 79             // 退出
 80             ftp.logout();
 81             result = true;
 82         } catch (IOException e) {
 83             e.printStackTrace();
 84             throw new Exception(e);
 85         } finally {
 86             if (ftp.isConnected()) {
 87                 try {
 88                     ftp.disconnect();
 89                 } catch (IOException ioe) {
 90                     ioe.printStackTrace();
 91                 }
 92             }
 93         }
 94         return result;
 95     }
 96 
 97     /**
 98      * Description: 从FTP服务器下载文件
 99      * 
100      * @param host
101      *            FTP服务器hostname
102      * @param port
103      *            FTP服务器端口
104      * @param username
105      *            FTP登录账号
106      * @param password
107      *            FTP登录密码
108      * @param remotePath
109      *            FTP服务器上的相对路径
110      * @param fileName
111      *            要下载的文件名
112      * @param localPath
113      *            下载后保存到本地的路径
114      * @return
115      * @throws Exception 
116      */
117     public static boolean downloadFile(String host, int port, String username, String password, String remotePath,
118             String fileName, String localPath) throws Exception {
119         boolean result = false;
120         FTPClient ftp = new FTPClient();
121         try {
122             int reply;
123             ftp.connect(host, port);
124             // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
125             ftp.login(username, password);// 登录
126             reply = ftp.getReplyCode();
127             if (!FTPReply.isPositiveCompletion(reply)) {
128                 ftp.disconnect();
129                 return result;
130             }
131             ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
132             FTPFile[] fs = ftp.listFiles();
133             for (FTPFile ff : fs) {
134                 if (ff.getName().equals(fileName)) {
135                     File localFile = new File(localPath + "/" + ff.getName());
136 
137                     OutputStream is = new FileOutputStream(localFile);
138                     ftp.retrieveFile(ff.getName(), is);
139                     is.close();
140                 }
141             }
142 
143             ftp.logout();
144             result = true;
145         } catch (IOException e) {
146             e.printStackTrace();
147             throw new Exception(e);
148         } finally {
149             if (ftp.isConnected()) {
150                 try {
151                     ftp.disconnect();
152                 } catch (IOException ioe) {
153                     ioe.printStackTrace();
154                 }
155             }
156         }
157         return result;
158     }
159 }
原文地址:https://www.cnblogs.com/bignew/p/6715639.html