- 浏览: 72115 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (113)
- oracle数据 (2)
- eclipse (3)
- 工具类使用 (14)
- 分隔符 (1)
- socket (1)
- ServletContextListener (1)
- String (3)
- StringUtils工具类 (2)
- springboot拦截器 (1)
- 页面 (2)
- map (1)
- 密码加密 (3)
- 缓存 (1)
- 文件上传 (1)
- 算法 (3)
- jquery (1)
- DateFormatUtils (1)
- xml (2)
- ftp (1)
- 接口 (3)
- 公钥私钥 (1)
- sigar (1)
- 前端 (2)
- lang3 (1)
- 定时器 (1)
- java基础 (13)
- javaBean (1)
- 工具类 (2)
- 插件 (1)
- 数据库 (2)
- 项目 (4)
- springboot (6)
- java集合 (1)
- 测试 (1)
- thymeleaf (3)
- mysql (7)
- 分布式 (1)
- idea (1)
- TCP (1)
- 微服务 (1)
- 高并发 (3)
- redis (1)
- 多线程 (2)
- SpringCloud (1)
- spring (1)
- 1111 (0)
- 开源 (1)
- npm (1)
最新评论
package com.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.SocketException; import java.util.ArrayList; import java.util.List; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.log4j.Logger; public class FtpUtil { public static final int BINARY_FILE_TYPE = FTP.BINARY_FILE_TYPE; public static final int ASCII_FILE_TYPE = FTP.ASCII_FILE_TYPE; /** 系统日志对象 */ private static Logger log = Logger.getLogger(FtpUtil.class); /** FTP client */ private FTPClient ftpClient; /** FTP Server name/ip */ private String server; /** FTP Server port */ private int port = 21; /** FTP Server login user */ private String user; /** FTP Server login password */ private String password; /** FTP Server working path */ private String path; /** 下载超时时间(毫秒)*/ private int downloadTimeout = 10000; public void setDownloadTimeout(int downloadTimeout) { this.downloadTimeout = downloadTimeout; } public int getDownloadTimeout() { return downloadTimeout; } /** * @param password the password to set */ public void setPassword(String password) { this.password = password; } /** * @param path the path to set */ public void setPath(String path) { this.path = path; } /** * @param port the port to set */ public void setPort(int port) { this.port = port; } /** * @param server the server to set */ public void setServer(String server) { this.server = server; } /** * @param user the user to set */ public void setUser(String user) { this.user = user; } public FtpUtil() { } /** * 创建ftp * @param ftpServer * @param ftpPort * @param ftpUser * @param ftpPwd * @param ftpPath */ public FtpUtil(String ftpServer, int ftpPort, String ftpUser, String ftpPwd, String ftpPath) { setServer(ftpServer); setPort(ftpPort); if (ftpPath != null && !"".equals(ftpPath)) setPath(ftpPath); setUser(ftpUser); setPassword(ftpPwd); } /** * ftp连接 * @throws SocketException * @throws IOException */ public void connectServer() throws SocketException, IOException { ftpClient = new FTPClient(); if (server != null && !(server.equals("")) && port != -1){ //ftpClient.setDefaultTimeout(downloadTimeout); ftpClient.setConnectTimeout(downloadTimeout); log.info("Connected to " + server + "."); ftpClient.connect(server, port); log.info(ftpClient.getReplyCode()); ftpClient.login(user, password); // Path is the sub-path of the FTP path if (path != null && path.length() != 0) { log.info("System encode :" + System.getProperty("file.encoding")); log.info("Ftp encode :" + ftpClient.getControlEncoding()); //编码转换支持中文路径 boolean chg = ftpClient.changeWorkingDirectory(new String(path.getBytes(),ftpClient.getControlEncoding())); log.info("change path:" + path + "[" + chg + "]"); } //设置编码 支持中文文件下载 ftpClient.setControlEncoding(System.getProperty("file.encoding")); } } // FTP.BINARY_FILE_TYPE | FTP.ASCII_FILE_TYPE // Set transform type public void setFileType(int fileType) throws IOException { ftpClient.setFileType(fileType); } public void closeServer() throws IOException { if (ftpClient != null && ftpClient.isConnected()) { ftpClient.disconnect(); } } // ======================================================================= // == About directory ===== // The following method using relative path better. // ======================================================================= public boolean changeDirectory(String path) throws IOException { return ftpClient.changeWorkingDirectory(path); } public boolean createDirectory(String pathName) throws IOException { return ftpClient.makeDirectory(pathName); } public boolean removeDirectory(String path) throws IOException { return ftpClient.removeDirectory(path); } // delete all subDirectory and files. public boolean removeDirectory(String path, boolean isAll) throws IOException { if (!isAll) { return removeDirectory(path); } FTPFile[] ftpFileArr = ftpClient.listFiles(path); if (ftpFileArr == null || ftpFileArr.length == 0) { return removeDirectory(path); } // for (FTPFile ftpFile : ftpFileArr) { String name = ftpFile.getName(); if (ftpFile.isDirectory()) { log.info("* [sD]Delete subPath [" + path + "/" + name + "]"); removeDirectory(path + "/" + name, true); } else if (ftpFile.isFile()) { log.info("* [sF]Delete file [" + path + "/" + name + "]"); deleteFile(path + "/" + name); } else if (ftpFile.isSymbolicLink()) { } else if (ftpFile.isUnknown()) { } } return ftpClient.removeDirectory(path); } // Check the path is exist; exist return true, else false. public boolean existDirectory(String path) throws IOException { boolean flag = false; FTPFile[] ftpFileArr = ftpClient.listFiles(path); for (FTPFile ftpFile : ftpFileArr) { if (ftpFile.isDirectory() && ftpFile.getName().equalsIgnoreCase(path)) { flag = true; break; } } return flag; } // ======================================================================= // == About file ===== // Download and Upload file using // ftpUtil.setFileType(FtpUtil.BINARY_FILE_TYPE) better! // ======================================================================= // #1. list & delete operation // Not contains directory public List<String> getFileList(String path) throws IOException { // listFiles return contains directory and file, it's FTPFile instance // listNames() contains directory, so using following to filer // directory. // String[] fileNameArr = ftpClient.listNames(path); FTPFile[] ftpFiles = ftpClient.listFiles(path); List<String> retList = new ArrayList<String>(); if (ftpFiles == null || ftpFiles.length == 0) { return retList; } for (FTPFile ftpFile : ftpFiles) { if (ftpFile.isFile()) { retList.add(ftpFile.getName()); } } return retList; } public boolean deleteFile(String pathName) throws IOException { return ftpClient.deleteFile(pathName); } // #2. upload to ftp server // InputStream <------> byte[] simple and See API public boolean uploadFile(String fileName, String newName) throws IOException { boolean flag = false; InputStream iStream = null; try { iStream = new FileInputStream(fileName); flag = ftpClient.storeFile(newName, iStream); } catch (IOException e) { flag = false; return flag; } finally { if (iStream != null) { iStream.close(); } } return flag; } public boolean uploadFile(String fileName) throws IOException { return uploadFile(fileName, fileName); } public boolean uploadFile(InputStream iStream, String newName) throws IOException { boolean flag = false; try { // can execute [OutputStream storeFileStream(String remote)] // Above method return's value is the local file stream. flag = ftpClient.storeFile(newName, iStream); } catch (IOException e) { flag = false; return flag; } finally { if (iStream != null) { iStream.close(); } } return flag; } // #3. Down load public boolean download(String remoteFileName, String localFileName) throws IOException { boolean flag = false; File outfile = new File(localFileName); OutputStream oStream = null; try { oStream = new FileOutputStream(outfile); flag = ftpClient.retrieveFile(remoteFileName, oStream); } catch (IOException e) { flag = false; return flag; } finally { oStream.close(); } return flag; } public InputStream downFile(String sourceFileName) throws IOException { return ftpClient.retrieveFileStream(sourceFileName); } public Logger getLog() { return log; } public void setLog(Logger log) { this.log = log; } }
工具类的使用
package com.ftp; import java.io.File; import java.io.IOException; import java.util.List; import org.apache.log4j.Logger; import com.util.FtpUtil; public class FTPTest { /** 系统日志对象 */ private static Logger logger = Logger.getLogger(FTPTest.class); public static void main(String[] args) throws IOException { //FTP下载文件 new FTPTest().downFile("B_100018_DTL01_20170614_0001.txt.gz","D:"+File.separator+"ftp"); //FTP上传文件 new FTPTest().uploadFile("F_100019_DTL02_20170615_0002.txt.gz","D:"+File.separator+"ftp"); } /** * FTP下载文件 * @param fileName 文件名称 * @param localPath 本地地址 * @throws IOException */ public void downFile(String fileName,String localPath) throws IOException{ String ftpServer ="172.16.10.55"; //properties.getProperty(FundChannelKey.FTP_SERVER); int ftpPort =21; //Integer.parseInt(properties.getProperty(FundChannelKey.FTP_PORT)); String ftpUser = "100018";//properties.getProperty(FundChannelKey.FTP_USER); String ftpPwd ="100018"; //properties.getProperty(FundChannelKey.FTP_PWD); String ftpPath ="/DTL01"; //properties.getProperty(FundChannelKey.FTP_PATH); boolean isDown = false; FtpUtil ftp = new FtpUtil(ftpServer, ftpPort, ftpUser, ftpPwd, ftpPath); //ftp.setLog(logger); try { //连接ftp服务器 System.out.println("开始连接ftp服务器" + ftpServer + ":" + ftpPort + ftpPath); ftp.connectServer(); System.out.println("ftp服务器连接成功"); //获取文件列表 List<String> fileList = ftp.getFileList(null); if (fileList != null && fileList.size() > 0) { System.out.println("共有文件[" + fileList.size() + "]个"); File localPathDir= new File(localPath); if(!localPathDir.exists()){ localPathDir.mkdirs(); } for (String file : fileList) { System.out.println("比较文件[" + file + "][" + fileName + "],匹配[" + file.equals(fileName) + "]"); if (file.equals(fileName)) { ftp.download(file, localPath + File.separator + fileName); //从服务器删除文件 //ftp.deleteFile(file); isDown = true; System.out.println("[ftp下载执行完成]"); break; } } } ftp.closeServer(); } catch (Exception e) { System.out.println("[对账文件下载失败][FTP异常]"+ e); throw e; } finally { ftp.closeServer(); System.out.println("[FTP关闭]"); } } /** * FTP上传文件 * @param fileName 文件名称 * @param localPath 本地地址 * @throws IOException */ public void uploadFile(String fileName,String localPath) throws IOException{ String ftpServer ="172.16.10.55"; //properties.getProperty(FundChannelKey.FTP_SERVER); int ftpPort =21; //Integer.parseInt(properties.getProperty(FundChannelKey.FTP_PORT)); String ftpUser = "100019";//properties.getProperty(FundChannelKey.FTP_USER); String ftpPwd ="100019"; //properties.getProperty(FundChannelKey.FTP_PWD); String ftpPath ="/"; //properties.getProperty(FundChannelKey.FTP_PATH); String uploadDir="STL01"; FtpUtil ftp = new FtpUtil(ftpServer, ftpPort, ftpUser, ftpPwd, ftpPath); //ftp.setLog(logger); try { //连接ftp服务器 System.out.println("开始连接ftp服务器" + ftpServer + ":" + ftpPort + ftpPath); ftp.connectServer(); System.out.println("ftp服务器连接成功"); //创建并改变目录位置 ftp.createDirectory(uploadDir); ftp.changeDirectory(uploadDir); //上传文件 if(ftp.uploadFile(localPath+File.separator+fileName,fileName)) System.out.println("上传文件成功,文件本地地址:"+localPath+File.separator+fileName); ftp.closeServer(); } catch (Exception e) { System.out.println("[对账文件下载失败][FTP异常]"+ e); throw e; } finally { ftp.closeServer(); System.out.println("[FTP关闭]"); } } }
相关推荐
ftp工具类,构造方法初始化类,连接ftp,列出所有文件内容,下载文件
FTP 工具类,赚钱积分下载东西,谢谢各位!谢谢各位!
ftp工具类,帮助你很容易的实现ftp功能
java操作FTP工具类:实现基本断点上传下载、实现上传下载进度汇报、实现中文目录创建及中文文件创建,添加对于中文的支持
FTP工具类,包括:文件夹上传下载,文件删除(包括非空文件夹),重命名等操作 基本的操作应该都有
在本文中,我们将深入探讨如何利用这个库开发一个FTP工具类,以便在Java应用程序中进行文件上传、下载和其他FTP操作。 首先,我们需要了解FTP的基本概念。FTP是一种用于在网络上进行文件传输的标准协议。它允许用户...
ftp操作工具类,用户ftp文件的添加,删除,等操作!
Java ftp工具类,可以实现ftp上传,读取,目录切换,内容创建,目录创建、检查文件是否存在,支持主动方式和被动方式读取
jdk1.7以上专用FTP工具类,本人花了半天时间调试通过,拿来即用,具体用法详见main函数。
对于初学者来说,理解并使用这样的FTP工具类可以帮助他们快速掌握FTP操作,避免了重复编写相同功能的代码。同时,这个工具类也展示了如何在Java中利用第三方库(如Apache Commons Net)来扩展Java标准库的功能。 ...
接下来,我们将深入探讨FTP工具类的主要功能、使用方法以及源码分析。 **1. FTPClient类** FTPClient是Apache Commons Net的核心类,它实现了FTP协议的大部分功能。通过这个类,我们可以连接到FTP服务器,执行登录...
本篇文章将详细讲解如何使用Java编写一个FTP工具类,实现连接FTP服务器、上传文件、删除文件、下载文件以及检索文件的功能。 首先,我们需要引入Apache Commons Net库,该库提供了丰富的FTP客户端API。在`pom.xml`...
Apache FTPClient操作FTP工具类
本话题将详细介绍如何使用Java实现FTP工具类以及所需的jar包。 Apache Commons Net是一个强大的Java网络实用程序库,它提供了多种网络协议的实现,包括FTP。在这个场景中,`commons-net-3.3.jar`是这个库的一个版本...
org.apache.commons.net.ftp.FTPClient FTP工具类,实现上传、下载、压缩到输出流下载等功能
Java FTP工具类是Java开发中用于处理FTP(File Transfer Protocol)协议的一种实用程序,它使得在Java应用程序中上传、下载、删除或者管理远程服务器上的文件变得简单。在本压缩包中,我们有一个名为"util"的文件,...
ftp 上传时,用到的工具类,项目上配置好ftp服务器后,controller可以方便的调用此工具类进行上传
ftp工具类,包含文件上传,文件删除,文件列表,查询当天文件类表方法
java的ftp工具类,需要的自行下载查看,有切换目录,创建目录方法。
本文将详细介绍标题和描述中提到的几个关键知识点:Java中的zip、rar(包括处理带密码的RAR文件)、gz压缩,以及FTP工具类的使用。 1. **Java ZIP压缩与解压缩**: Java内置的`java.util.zip`包提供了处理ZIP文件...