FTPUtil

  1 package com.jf.utils;
  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.OutputStream;
  8 
  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 public class FtpUtils {
 15     //private static FTPClient ftpclient = null;
 16     private static String ip = PropertiesUtils.props.get("ip").toString();
 17     private static String userName = PropertiesUtils.props.get("userName").toString();
 18     private static String passWord = PropertiesUtils.props.get("passWord").toString();
 19     private static ThreadLocal<FTPClient> ftpClientThreadLocal = new ThreadLocal<FTPClient>();
 20     //static{
 21     //ftpclient = getFtpClient(ip, userName, passWord);
 22     //}
 23     public static FTPClient getFtpClient() {
 24         if(ftpClientThreadLocal.get() != null && ftpClientThreadLocal.get().isConnected()){
 25             return ftpClientThreadLocal.get();
 26         }else{
 27             FTPClient ftpClient = new FTPClient();
 28             // 传输的字符编码(防乱码)
 29             ftpClient.setControlEncoding("UTF-8");
 30             // 超时时间(防止FTP僵死)
 31             ftpClient.setConnectTimeout(1000 * 60);
 32             ftpClient.setDataTimeout(1000 * 60);
 33             ftpClient.setDefaultTimeout(1000 * 60);
 34             // 工作流的大小
 35             ftpClient.setBufferSize(1024 * 4);
 36             // 主被动模式(应对FTP服务器的相关安全设置)
 37             // ftpclient.enterLocalPassiveMode();
 38             ftpClient.enterLocalActiveMode();
 39             @SuppressWarnings("unused")
 40             FTPFile[] ftpFiles = null;
 41             // 设置连接ip
 42             try {
 43                 ftpClient.connect(ip);
 44                 // 连接是否正常
 45                 if (ftpClient.isConnected()) {
 46                     // 使用用户名和密码进行登录
 47                     if (ftpClient.login(userName, passWord)) {
 48                         // 判断FTP连接是否可用、源码中判断ReplyCode值为(reply >= 200) && (reply <
 49                         // 300)为可用,非单纯200!
 50                         if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
 51                             System.out.println("FTP登录成功,FTP IP:"+ ftpClient.getRemoteAddress());
 52                             setFileType(ftpClient);
 53                         }
 54                     }
 55                 }
 56             } catch (Exception e) {
 57                 e.printStackTrace();
 58             }
 59             return ftpClient;
 60         }
 61     }
 62     
 63     /**
 64     * 设置文件类型
 65     * @param ftpClient
 66     */
 67     @SuppressWarnings("static-access")
 68     public static void setFileType(FTPClient ftpClient){
 69         try {
 70             // 默认为ASCII(value:0),此处设置为2进制
 71             System.out.println("设置文件类型为:"+ ftpClient.BINARY_FILE_TYPE);
 72             ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
 73             ftpClient.changeWorkingDirectory("/");
 74         } catch (IOException e1) {
 75             e1.printStackTrace();
 76         }
 77     }
 78     
 79     /**
 80     * 断开FTP连接
 81     */
 82     public static void disconnect(){
 83         try {
 84             FTPClient ftpClient = getFtpClient();
 85             ftpClient.logout();
 86             if(ftpClient.isConnected()){
 87             ftpClient.disconnect();
 88             ftpClient = null;
 89         }
 90         } catch (IOException e) {
 91             e.printStackTrace();
 92         }
 93     }
 94     
 95     /**
 96     * 上传
 97     * @param client
 98     * @param file 上传的文件
 99     * @param uploadPath 服务器路径
100     * @return
101     */
102     public static boolean upload(File file,String serverPath) {
103         FTPClient ftpClient = getFtpClient();
104         // 上传文件到FTP服务器
105         // 扫描本地文件
106         // 获取当前目录下的所有文件
107         FileInputStream fis = null;
108         String name = null;
109         boolean sto = true;
110         try {
111             ftpClient.changeWorkingDirectory(serverPath);
112             name = file.getName();
113             System.out.println("上传本地文件:" + name);
114             fis = new FileInputStream(file.getPath());
115             sto = ftpClient.storeFile(name, fis);
116         } catch (Exception e) {
117             e.printStackTrace();
118             sto = false;
119         }finally{
120         }
121         if(sto)
122             System.out.println("上传" + name + "成功");
123         else
124             System.out.println("上传" + name + "失败");
125         return sto;
126     }
127     
128     /**
129     * 下载
130     * @param client
131     * @param filename  下载的文件名
132     * @param serverPath  下载文件的路径
133     * @param localPath  保存到本地的路径
134     * @return
135     */
136     public static boolean download(String filename,String serverPath,String localPath){
137         FTPClient ftpClient = getFtpClient();
138         boolean ret = true;
139         try {
140             ftpClient.changeWorkingDirectory(serverPath);
141             OutputStream os = null;
142             System.out.println("下载FTP文件:" + filename);
143             os = new FileOutputStream(localPath + "/"+ filename);
144             ret = ftpClient.retrieveFile(filename, os);
145         } catch (Exception e) {
146             e.printStackTrace();
147             ret = false;
148         }
149         if(ret)
150             System.out.println("下载" + filename + "成功");
151         else
152             System.out.println("下载" + filename + "失败");
153         return ret;
154     }
155     
156     /**
157     * 删除
158     * @param filename  文件名
159     * @param serverPath  文件的服务器路径
160     * @return
161     */
162     public static boolean delete(String filename,String serverPath){
163         FTPClient ftpClient = getFtpClient();
164         boolean del = true;
165         try {
166             ftpClient.changeWorkingDirectory(serverPath);
167             del = ftpClient.deleteFile(filename);
168         } catch (Exception e) {
169             e.printStackTrace();
170             del = false;
171         }
172         if(del)
173             System.out.println("删除" + filename + "成功");
174         else
175             System.out.println("删除" + filename + "失败");
176         return del;
177     }
178     
179     /**
180     * 判断本地文件是否存在
181     * @param filePath 文件路径 以“/”结尾
182     * @param fileName 文件名
183     * @return
184     */
185     public static boolean judgeFile(String filePath,String fileName){
186         File file = new File(filePath + fileName);
187         return file.exists();
188     }
189     
190     /**
191     * 判断FTP服务器上的文件是否存在 0为不存在,-1为异常
192     * @param fileServerPath 文件的服务器路径
193     * @param fileName  文件名
194     * @return
195     */
196     public static int judgeServerFile(String fileServerPath,String fileName){
197         FTPClient ftpClient = getFtpClient();
198         int num = -1;
199         try {
200             ftpClient.changeWorkingDirectory(fileServerPath);
201             FTPFile[] ftpFiles = ftpClient.listFiles(fileName);
202             num = ftpFiles.length;
203         } catch (IOException e) {
204             e.printStackTrace();
205         }
206         return num;
207     }
208     
209     public static void main(String[] args) {
210         //File file = new File("D://ftp_test.txt");
211         //上传
212         //FtpUtils.upload(file, "/test");
213         //下载
214         FtpUtils.download("ftp","/xxx/","D://aaa");
215         //删除
216         //FtpUtils.delete("ftp_test.txt","/test");
217         //System.out.println(FtpUtils.judgeFile("D://aaa/", "ftp_test.txt"));
218     }
219 }

 

posted @ 2018-09-16 22:26  面向bug编程  阅读(291)  评论(0编辑  收藏  举报