<!---Maven依赖->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-ftp</artifactId>
<version>2.13.2</version>
</dependency>
package com.ftp; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.commons.io.IOUtils; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; public class FtpUtil { // ftp配置相关信息,可封装成一个对象,也可以做成配置文件 private static FTPClient ftpClient = null; private static String hostname = "92kk.top"; private static int port = 21; private static String username = "chenwk"; private static String password = "111111"; private static boolean isLogin = false; public FtpUtil() { ftpClient = new FTPClient(); // 连接FTP服务器 try { ftpClient.connect(hostname, port); //设置文件传输类型,common-net版本低于2.2 只能在登录之前设置fileType,否则报连接超时 ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // 登录FTP服务器 ftpClient.login(username, password); // 验证FTP服务器是否登录成功 int replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) { System.out.println("登录失败!"); } else { System.out.println("登录成功!"); isLogin = true; } } catch (IOException e) { e.printStackTrace(); } } public static FtpUtil newInstance() { return new FtpUtil(); } /** * 下载ftp目录下的所有文件到指定目录 * * @param localpath * @param ftpDir * @param isCreateRootDir 是否创建指定的根目录 */ public void downloadDirFiles(String localpath, String ftpDir , boolean isCreateRootDir , List excelList) { if (isLogin) { if(!isCreateRootDir) { isCreateRootDir = true; }else{ localpath += "/"+ftpDir; localpath=localpath.replace("//", "/"); } File dir = new File(localpath); if (!dir.exists()) { dir.mkdirs(); } try { ftpClient.changeWorkingDirectory(ftpDir); FTPFile[] ftpFiles = ftpClient.listFiles(); for (FTPFile file : ftpFiles) { //防止中文乱码 String fileName = new String(file.getName().getBytes("iso8859-1"), "UTF-8"); if (file.isFile()) { File targetFile = new File(localpath + "/" + fileName); String targetFilePath = targetFile.getAbsolutePath(); System.out.println("下载文件:" + targetFilePath); FileOutputStream os = new FileOutputStream(targetFile); ftpClient.retrieveFile(fileName, os); IOUtils.closeQuietly(os); if(targetFilePath.contains(".xls") || targetFilePath.contains(".xlsx")){ excelList.add(targetFilePath); } } if(file.isDirectory()){ // 目录 ,递归调用 downloadDirFiles(localpath, fileName + "/" , isCreateRootDir,excelList); } } //当前目录下载完成,返回上级目录 if (!"".equals(ftpDir)) ftpClient.changeWorkingDirectory(".."); } catch (Exception e) { e.printStackTrace(); } } else { System.out.println("登录ftp失败,请检查登录配置!"); } } /** * 递归删除指定目录的所有文件,包括文件夹 * @param ftpDir * @return int 成功删除的记录 */ public int deleteDirFiles(String ftpDir){ int count = 0; try { ftpClient.changeWorkingDirectory(ftpDir); FTPFile[] ftpFiles = ftpClient.listFiles(); for (FTPFile file : ftpFiles) { //防止中文乱码 String fileName = new String(file.getName().getBytes("iso8859-1"), "UTF-8"); if (file.isFile()) { boolean f = ftpClient.deleteFile(fileName); if(f){ System.out.println("删除文件-成功:" + fileName); }else{ System.out.println("删除文件-失败:" + fileName); count++; } } else if(file.isDirectory()){ //递归调用 deleteDirFiles(fileName + "/"); boolean f = ftpClient.removeDirectory(fileName); if(f){ System.out.println("删除文件夹-成功:" + fileName); count++; }else{ System.out.println("删除文件夹-失败:" + fileName); } } } //当前目录删除完成,返回上级目录 if (!"".equals(ftpDir)){ ftpClient.changeWorkingDirectory(".."); } } catch (IOException e) { e.printStackTrace(); } return count; } /** * 读取指定目录下的所有文件文件名 * * @param pathname * @return * @throws IOException */ public List getFileList(String pathname) { List fileLists = new ArrayList(); if (isLogin) { // 获得指定目录下所有文件名 FTPFile[] ftpFiles = null; try { ftpFiles = ftpClient.listFiles(pathname); for (int i = 0; ftpFiles != null && i < ftpFiles.length; i++) { FTPFile file = ftpFiles[i]; if (file.isFile()) { fileLists.add(new String(file.getName().getBytes("iso8859-1"), "UTF-8")); } } } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("登录ftp失败,请检查登录配置!"); } return fileLists; } /** * 退出登录 */ public void logout() { if (ftpClient.isConnected()) { try { if (ftpClient != null) ftpClient.logout(); System.out.println("退出登录!"); } catch (IOException e) { e.printStackTrace(); } } } }
Main方法测试类
public static void main(String[] args) { //指定ftp目录 String pathname = "/product/"; FtpUtil ftpUtil = FtpUtil.newInstance(); List excelList = new ArrayList<>(); ftpUtil.downloadDirFiles("E:\\downFtpPath",pathname,true,excelList); ftpUtil.deleteDirFiles(pathname); System.out.println( excelList.toString()); ftpUtil.logout(); }
转载请注明:左手代码右手诗 » 使用FTPClient批量下载、删除Ftp文件