package com.ctl.util;
//需要commons-net-3.0.1.jar
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.net.ftp.*;
import com.ctl.util.exception.FTPClientException;public class FTPClientUtil {/** logger */private static ThreadLocal<FTPClient> ftpClientThreadLocal;// ftp ip addressprivate static String host;// \u7AEF\u53E3\u53F7private static int port;private static String username;private static String password;private static boolean binaryTransfer;private static boolean passiveMode;// \u7F16\u7801private static String encoding;// \u8BBE\u7F6E\u8FDE\u63A5\u8D85\u65F6\u65F6\u95F4private static int clientTimeout;// \u67E5\u8BE2\u8FDC\u7A0B\u76EE\u5F55\u6240\u6709\u7684file nameprivate static List<String> listFileNames;// private static String filePathOfFtpserver;static {ftpClientThreadLocal = new ThreadLocal<FTPClient>();host = ConfigUtils.getType("ftp.host");port = Integer.parseInt(ConfigUtils.getType("ftp.port"));username = ConfigUtils.getType("ftp.username");password = ConfigUtils.getType("ftp.password");binaryTransfer = Boolean.parseBoolean(ConfigUtils.getType("ftp.binaryTransfer"));passiveMode = Boolean.parseBoolean(ConfigUtils.getType("ftp.passiveMode"));encoding = ConfigUtils.getType("ftp.encoding");clientTimeout = Integer.parseInt(ConfigUtils.getType("ftp.clientTimeout"));listFileNames = new ArrayList<String>();}/*** @description \u83B7\u53D6FTP\u5BA2\u6237\u7AEF\u8FDE\u63A5* @return* @throws FTPClientException* @throws SocketException* @throws IOException*/private static FTPClient getFTPClient() throws FTPClientException,SocketException, IOException {if (ftpClientThreadLocal.get() != null&& ftpClientThreadLocal.get().isConnected()) {return ftpClientThreadLocal.get();} else {FTPClient ftpClient = new FTPClient(); // \u6784\u9020\u4E00\u4E2AFtpClient\u5B9E\u4F8BftpClient.setControlEncoding(encoding); // \u8BBE\u7F6E\u5B57\u7B26\u96C6ftpClient.setConnectTimeout(clientTimeout);ftpClient.connect(host, port);// \u8FDE\u63A5\u540E\u68C0\u6D4B\u8FD4\u56DE\u7801\u6765\u6821\u9A8C\u8FDE\u63A5\u662F\u5426\u6210\u529Fint reply = ftpClient.getReplyCode();if (FTPReply.isPositiveCompletion(reply)) {// \u767B\u9646\u5230ftp\u670D\u52A1\u5668ftpClient.login(username, password);setFileType(ftpClient); // \u8BBE\u7F6E\u6587\u4EF6\u4F20\u8F93\u7C7B\u578B} else {ftpClient.disconnect();}if (passiveMode) {ftpClient.enterLocalPassiveMode();}ftpClientThreadLocal.set(ftpClient);return ftpClient;}}/*** @description\u8BBE\u7F6E\u6587\u4EF6\u4F20\u8F93\u7C7B\u578B* @throws FTPClientException* @throws IOException*/private static void setFileType(FTPClient ftpClient)throws FTPClientException, IOException {if (binaryTransfer) {ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);} else {ftpClient.setFileType(FTPClient.ASCII_FILE_TYPE);}}/*** @description\u65AD\u5F00ftp\u8FDE\u63A5* @throws FTPClientException* @throws IOException*/public static void disconnect() throws FTPClientException, IOException {FTPClient ftpClient = getFTPClient();ftpClient.logout();if (ftpClient.isConnected()) {ftpClient.disconnect();ftpClient = null;ftpClientThreadLocal.set(ftpClient);}}/*** @description \u6279\u91CF\u5220\u9664\u6240\u6709\u76EE\u5F55\u4E0B\u7684\u5BF9\u5E94\u7684\u6587\u4EF6* @param delFiles* @return* @throws FTPClientException* @throws IOException*/public static boolean deleteRemoteFiles(String[] delFiles)throws FTPClientException, IOException {List<String> list=listNames();//\u83B7\u53D6\u6240\u6709\u7684\u6587\u4EF6\u540Dfor(String filename:delFiles){for(Iterator<String> it=list.iterator();it.hasNext();){String filepath=it.next();if(filepath.contains(filename)){//\u5982\u679C\u8BE5\u8DEF\u5F84\u5305\u542B\u8BE5\u6587\u4EF6\u540D\u5219\u5220\u9664boolean result= getFTPClient().deleteFile(filepath);if(!result){return result;}}}}return true;}/*** @description \u5217\u51FA\u8FDC\u7A0B\u9ED8\u8BA4\u76EE\u5F55\u4E0B\u6240\u6709\u7684\u6587\u4EF6* @return \u8FDC\u7A0B\u9ED8\u8BA4\u76EE\u5F55\u4E0B\u6240\u6709\u6587\u4EF6\u540D\u7684\u5217\u8868\uFF0C\u76EE\u5F55\u4E0D\u5B58\u5728\u6216\u8005\u76EE\u5F55\u4E0B\u6CA1\u6709\u6587\u4EF6\u65F6\u8FD4\u56DE0\u957F\u5EA6\u7684\u6570\u7EC4* @throws FTPClientException* @throws IOException*/public static List<String> listNames() throws FTPClientException,IOException {return listNames(null);}public static List<String> listNames(String remotePath)throws FTPClientException, IOException {return listNames(remotePath, true);}/*** @description\u5217\u51FA\u8FDC\u7A0B\u76EE\u5F55\u4E0B\u6240\u6709\u7684\u6587\u4EF6* @param remotePath*            \u8FDC\u7A0B\u76EE\u5F55\u540D* @param autoClose*            \u662F\u5426\u81EA\u52A8\u5173\u95ED\u5F53\u524D\u8FDE\u63A5* @return \u8FDC\u7A0B\u76EE\u5F55\u4E0B\u6240\u6709\u6587\u4EF6\u540D\u7684\u5217\u8868\uFF0C\u76EE\u5F55\u4E0D\u5B58\u5728\u6216\u8005\u76EE\u5F55\u4E0B\u6CA1\u6709\u6587\u4EF6\u65F6\u8FD4\u56DE0\u957F\u5EA6\u7684\u6570\u7EC4* @throws FTPClientException* @throws IOException*/public static List<String> listNames(String remotePath,boolean containSubdirectory) throws FTPClientException, IOException {if (null == remotePath) {remotePath = "." + File.separator;}try {FTPFile[] files = getFTPClient().listFiles(remotePath);if (files.length < 3) {return listFileNames;}for (FTPFile file : files) {if (!file.getName().equals(".") && !file.getName().equals("..")) {if (file.isFile()) {// \u6587\u4EF6listFileNames.add("." + File.separator + file.getName());} else {// \u76EE\u5F55listNames2(remotePath + file.getName() + File.separator,containSubdirectory);}}}} catch (IOException e) {throw new FTPClientException("\u5217\u51FA\u8FDC\u7A0B\u76EE\u5F55\u4E0B\u6240\u6709\u7684\u6587\u4EF6\u65F6\u51FA\u73B0\u5F02\u5E38", e);}return listFileNames;}//listNames2\u9012\u5F52\u65B9\u6CD5private static void listNames2(String remotePath,boolean containSubdirectory) throws FTPClientException {try {FTPClient client = getFTPClient();client.changeWorkingDirectory(remotePath);FTPFile[] files = client.listFiles(remotePath);if (files.length < 3) {return;}for (FTPFile file : files) {if (!file.equals(".") && !file.equals("..")) {if (file.isFile()) {listFileNames.add(remotePath + file.getName());}if (file.isDirectory() && (!".".equals(file.getName()))&& (!"..".equals(file.getName()))) {String path = remotePath + file.getName()+ File.separator;listNames2(path, containSubdirectory);}}}} catch (IOException e) {throw new FTPClientException("\u5217\u51FA\u8FDC\u7A0B\u76EE\u5F55\u4E0B\u6240\u6709\u7684\u6587\u4EF6\u65F6\u51FA\u73B0\u5F02\u5E38", e);}}/*** * @param remotePath*            \u8FDC\u7A0B\u8DEF\u5F84* @param fileName*            \u8981\u4E0A\u4F20\u7684\u6587\u4EF6\u540D* @param localInputStream*            \u672C\u5730InputStream\u6D41* @return* @throws IOException* @throws FTPClientException*/public static boolean uploadToRemote(String remotePath, String fileName,InputStream localInputStream) throws IOException,FTPClientException {remotePath=remotePath+File.separator;FTPClient client = getFTPClient();int reply;reply = client.getReplyCode();if (!FTPReply.isPositiveCompletion(reply)) {client.disconnect();}client.makeDirectory(remotePath);//\u5728\u670D\u52A1\u7AEF\u5EFA\u7ACB\u8BE5\u6587\u4EF6\u5939client.changeWorkingDirectory(remotePath);boolean result = client.storeFile(remotePath+fileName, localInputStream);localInputStream.close();return result;}/*** * @param remotePath*            \u8FDC\u7A0B\u8DEF\u5F84* @param localPath*            \u672C\u5730\u8DEF\u5F84* @return* @throws IOException* @throws FTPClientException* @throws IOException*/public static boolean downloadToLocal(String remotePath, String localPath)throws IOException, FTPClientException, IOException {return downloadToLocal(remotePath, localPath, null);}/*** * @param remotePath*            \u8FDC\u7A0B\u8DEF\u5F84* @param localPath*            \u8981\u4E0B\u8F7D\u7684\u8DEF\u5F84* @param fileNames*            \u6240\u6709\u8981\u4E0B\u8F7D\u7684\u6587\u4EF6\u540D\u5B57* @return* @throws IOException* @throws FTPClientException* @throws IOException*/public static boolean downloadToLocal(String remotePath, String localPath,String[] fileNames) throws IOException, FTPClientException,IOException {remotePath=remotePath+File.separator;localPath=localPath+File.separator;FTPClient client = getFTPClient();client.changeWorkingDirectory(remotePath);FTPFile[] ftpList = client.listFiles(remotePath);boolean result = true;if (null == fileNames) {for (FTPFile f : ftpList) {if (f.getSize() > 0) {File file = new File(localPath);file.mkdirs();OutputStream out = new FileOutputStream(localPath + f.getName());result = client.retrieveFile(f.getName(), out); // \u4E0B\u8F7Dout.close();if (!result) {break;}}}} else {for (String fileName : fileNames) {File file = new File(localPath);file.mkdirs();OutputStream out = new FileOutputStream(localPath+ File.separator + fileName);result = client.retrieveFile(fileName, out); // \u4E0B\u8F7Dout.close();if (!result) {break;}}}return result;}/*** @param client* @param fileName*            \u8FDC\u7A0B\u8DEF\u5F84\u540D* @return* @throws IOException* @throws FTPClientException*/public static int getRemoteFileSize(String fileName) throws IOException,FTPClientException {FTPClient client = getFTPClient();int size = 0;FTPFile[] ftpList = client.listFiles();for (FTPFile f : ftpList) {if (f.getName().equalsIgnoreCase(fileName)) {size = (int) f.getSize();}}return size;}/*** * @param filename*            \u8981\u4E0B\u8F7D\u7684\u6587\u4EF6\u540D \u4ECE\u6574\u4E2A\u670D\u52A1\u5668\u4E2D\u67E5\u627E,\u53EF\u80FD\u627E\u5230\u591A\u4E2A\u76F8\u540C\u540D\u5B57\u7684\u6587\u4EF6,\u6309\u5728\u670D\u52A1\u7AEF\u7684\u8DEF\u5F84\u5728\u6307\u5B9A\u672C\u5730\u8DEF\u5F84\u4E0B\u521B\u5EFA\u60F3\u5BF9\u5E94\u7684\u8DEF\u5F84\u548C\u6587\u4EF6* @param localPath*            \u672C\u5730\u8DEF\u5F84* @return* @throws Exception */public static boolean downloadToLocal2(String filename, String localPath)throws Exception {List<String> list=listNames();OutputStream out;try{for(Iterator<String> it=list.iterator();it.hasNext();){String filepath=it.next();if(filepath.contains(filename)){String remoteFilePath=filepath.substring(1, filepath.length());File file=new File(localPath+remoteFilePath);new File(file.getParent()).mkdirs();out= new FileOutputStream(localPath+remoteFilePath);getFTPClient().retrieveFile(filepath, out); // \u4E0B\u8F7Dout.close(); }}return true;}catch (Exception e) {return false;}}/*** @description \u521B\u5EFA\u8FDC\u7A0B\u76EE\u5F55\u5141\u8BB8\u521B\u5EFA\u591A\u7EA7\u76EE\u5F55* @param remoteDir \u8FDC\u7A0B\u76EE\u5F55* @return* @throws SocketException* @throws IOException* @throws FTPClientException*/public static boolean mkdirs(String remoteDir) throws SocketException, IOException, FTPClientException{String[] dirs=remoteDir.split("/");String remotePath=".";for(String dir:dirs){if(!dir.equals(".")&&null!=dir){remotePath=remotePath+File.separator+dir+File.separator;boolean result=getFTPClient().makeDirectory(remotePath);if(!result){return result;}}}return true;}
}

FTPClientUtil FTP客户端工具相关推荐

  1. IIS7批量FTP客户端工具软件

    简介: IIS7批量FTP客户端工具软件作为一款非常专业的服务器管理工具,为用户提供了批量管理ftp上传下载功能,软件支持自动重连.自动重传.定时上传.定时下载等功能,大量文件快速加载,边加载边传输, ...

  2. filezilla检查新版本mac_FileZilla for Mac(ftp客户端工具)中文版

    原标题:FileZilla for Mac(ftp客户端工具)中文版 filezilla mac 中文版是一款运行在Mac OS平台上的FTP上传下载工具,filezilla mac版具备所有的FTP ...

  3. gFTP - 多线程 FTP 客户端工具

    gFTP - 多线程 FTP 客户端工具 https://www.gftp.org/ gFTP is a free multithreaded file transfer client for *NI ...

  4. 7个免费的Linux FTP客户端工具

    在Dropbox.YouSendIt.idrive以及许多这样云存储和共享工具的帮助下,我们在互联网上发送和共享大型文件变得容易起来.所有这些网站都可以帮助你在互联网上传送文件,但如果你要分享庞大的数 ...

  5. Apache Ftp客户端工具FtpClient

    引入依赖 <dependency><groupId>commons-net</groupId><artifactId>commons-net</a ...

  6. windows ftp服务器_ftp客户端软件,推荐6个流行的FTP客户端软件

    无论你是做网站工作,还是运行一个家庭FTP服务器,或者你只是喜欢高速下载,一个稳定且功能齐全的FTP客户端工具都可以节省你大量时间和生命,现在有大量的免费或者收费的FTP客户端软件供大家选择,这里总结 ...

  7. ftp下载工具绿色版,网络上难找的绿色版ftp下载工具

    网络上最不好找的就是绿色版的工具,毕竟在现在这个年代不可能让你免费使用别人的产品,所以绿色版的工具是真的难找.相信常常使用电脑的小伙伴也都知道,绿色版的软件都不好找,更何况是ftp工具绿色版,ftp下 ...

  8. ftp 下载 工具,5款小白都能使用的ftp 下载 工具

    ftp下载工具,一看这名字就知道这个软件是用来下载的,但是它还有一个上传的功能,可谓是方便的很,有的功能使用起来特别复杂,接下来给大家介绍5款小白都能使用的ftp下载 工具. 第一款:IIS7服务器管 ...

  9. C#毕业设计——基于C#+asp.net+FTP的FTP客户端设计与实现(毕业论文+程序源码)——FTP客户端

    基于C#+asp.net+FTP的FTP客户端设计与实现(毕业论文+程序源码) 大家好,今天给大家介绍基于C#+asp.net+FTP的FTP客户端设计与实现,文章末尾附有本毕业设计的论文和源码下载地 ...

最新文章

  1. python常用的内置函数
  2. 张量功率谱CAMB参数调试
  3. WPF Binding
  4. HDU中一些DP的题目分类
  5. 【PAT - 1014】福尔摩斯的约会(简单模拟)
  6. redis创建像mysql表结构_Redis数据结构列表实现
  7. 排序 oracle,oracle排序
  8. Linux文件压缩,解压常用命令
  9. http重定向到https
  10. c语言实现简单俄罗斯方块
  11. 在计算机中dos代表什么意思,dos是什么意思?怎么进DOS命令的方法
  12. 8.0高等数学五-斯托克斯公式
  13. ubuntu20.04基础入门日记V1.0
  14. 微信小程序--实现番茄钟功能
  15. 码农的2019又开始了,抓不住重点的我很悲催
  16. 上海电力学院计算机院校区,上海电力学院有几个校区及校区地址 哪个校区最好...
  17. 湛江各县市区5G建设计划已明确,共计7460座5G基站
  18. 国外免费的网站空间/免费主机
  19. Python之文件 打开与关闭
  20. Python学习起来难不难?

热门文章

  1. ISO/IEC14443
  2. 输入数据练习-JAVA
  3. 【Mathematica】三种画爱心的方法
  4. HyperLynx(十三)Hyperlynx前放置之对网络的EMC分析
  5. 腾讯云老用户重新注册新账号算新用户吗?
  6. python+selenium自动化脚本例子
  7. bash: setenv: command not found
  8. 阿里云【达摩院特别版·趣味视觉AI训练营】笔记2
  9. 斗地主洗牌发牌——排序版
  10. 微星显卡拷机测试软件,微星显卡拷机软件(MSI Kombustor)