package com.uas.util; import com.jcraft.jsch.JSchException; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; import java.io.*; import java.util.*; public class FtpUtil { /** * 连接到ftp站点,判断是否可以连接 * @param path 要连接到的目录 * @param addr 站点地址 * @param port 端口 * @param username 用户名 * @param password 密码 * @return * @throws Exception */ public static boolean connect(String path, String addr, int port, String username, String password) { boolean result = false; try { FTPClient ftp = new FTPClient(); int reply; ftp.connect(addr, port); ftp.login(username, password); ftp.setFileType(FTPClient.BINARY_FILE_TYPE); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } ftp.changeWorkingDirectory(path); result = true; return result; }catch (Exception e){ SFTPUtil sftp = new SFTPUtil(username, password, addr, port); try { sftp.login(); result = true; } catch (JSchException e1) { e1.printStackTrace(); } return result; } } /** * 获取ftp连接,该连接指向ftp配置中的downloadpath文件夹 * @param map 包含ftp连接配置的map */ public static FTPClient connect(Map map) throws Exception { return connect(map,map.get("downloadpath").toString()); } /** * 获取ftp连接,该连接指向ftp配置中的downloadpath文件夹 * @param map 包含ftp连接配置的map */ public static FTPClient connect(Map map,String path){ FTPClient ftp = null; String addr = null; try{ addr = map.get("ip").toString(); int port = Integer.parseInt(map.get("port").toString()); String username = map.get("user").toString(); String password = map.get("password").toString(); ftp = new FTPClient(); int reply; ftp.setDefaultTimeout(30*1000); ftp.setConnectTimeout(30*1000); ftp.setDataTimeout(30*1000); //设置ftp为被动模式,解决有时候ftp会卡住问题 ftp.enterLocalPassiveMode(); ftp.connect(addr, port); if(!ftp.login(username, password)){ return null; } ftp.setFileType(FTPClient.BINARY_FILE_TYPE); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return null; } ftp.changeWorkingDirectory(path); return ftp; }catch(Exception e){ BaseUtil.getLogger().error("ip:" + addr + e.toString()); e.printStackTrace(); return null; } } /** * 上传文件 * @param ftpClient 已连接的ftp客户端 * @param pathname 路径 * @param file 待上传的文件 * @return 上传结果 */ public static boolean uploadFile(FTPClient ftpClient, String pathname, File file) { boolean flag = false; ftpClient.setControlEncoding("UTF-8"); try { ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); boolean change = ftpClient.changeWorkingDirectory("/"+pathname); if(!change){ BaseUtil.getLogger().info("change to " + pathname + " fail"); return false; } InputStream inputStream = new FileInputStream(file); ftpClient.enterLocalPassiveMode(); ftpClient.storeFile(file.getName(), inputStream); inputStream.close(); //ftpClient.logout(); flag = true; } catch (Exception e) { e.printStackTrace(); BaseUtil.getLogger().error(e.toString()); if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException e1) { BaseUtil.getLogger().error(e1.toString()); e1.printStackTrace(); } } } return flag; } /** * 删除后关闭ftp连接 * @param ftpClient * @param filename 要删除的文件名 * @return 删除结果 */ public static boolean deleteFile(FTPClient ftpClient,String filename) { boolean flag = false; try { ftpClient.dele(filename); flag = true; } catch (Exception e) { BaseUtil.getLogger().error(e.toString()); e.printStackTrace(); } return flag; } /** * * @param ftpClient * fileType 需要下载的文件类型 * @return 获取连接到的ftp站点下的文件夹所有文件 */ public static List downloadAllFileByType(FTPClient ftpClient,String fileType) { List files = new ArrayList(); try { ftpClient.enterLocalPassiveMode(); FTPFile[] ftpFiles = ftpClient.listFiles(); if (ftpFiles != null && ftpFiles.length > 0) { ArrayList list = new ArrayList(Arrays.asList(ftpFiles)); Collections.sort(list, new Comparator() { @Override public int compare(FTPFile o1, FTPFile o2) { long time = o1.getTimestamp().getTime().getTime(); long time1 = o2.getTimestamp().getTime().getTime(); if (time > time1) { return 1; } if (time == time1) { return 0; } return -1; } }); for (FTPFile file : list) { if (file.getName().toUpperCase().endsWith("." + fileType)) { File localFile = new File(System.getProperty("java.io.tmpdir") + File.separator + file.getName()); OutputStream os = new FileOutputStream(localFile); ftpClient.retrieveFile(file.getName(), os); os.close(); files.add(localFile); } } } } catch (Exception e) { BaseUtil.getLogger().error(e.toString()); e.printStackTrace(); } return files; } /** * * @param ftpClient * fileType 需要下载的文件类型 * @return 获取连接到的ftp站点下的文件夹所有文件 */ public static List downloadAllFileByType(FTPClient ftpClient) { List files = new ArrayList(); try { ftpClient.enterLocalPassiveMode(); FTPFile[] ftpFiles = ftpClient.listFiles(); for (FTPFile file : ftpFiles) { if(!".".equals(file.getName()) && !"..".equals(file.getName())&& !"bak".equals(file.getName())){ File localFile = new File(System.getProperty("java.io.tmpdir") + File.separator + file.getName()); OutputStream os = new FileOutputStream(localFile); ftpClient.retrieveFile(file.getName(), os); os.close(); files.add(localFile); } } } catch (Exception e) { BaseUtil.getLogger().error(e.toString()); e.printStackTrace(); } return files; } /** * 关闭ftp连接 * @param ftpClient */ public static void closeFtpClient(FTPClient ftpClient){ if (ftpClient.isConnected()) { try { ftpClient.logout(); } catch (IOException e) { BaseUtil.getLogger().error(e.toString()); } } } /** * * @param ftpClient * @return 获取连接到的ftp站点下的文件夹所有文件 */ public static List downloadFileByDirectory(FTPClient ftpClient,String directory,String fileType) { List files = new ArrayList(); try { ftpClient.enterLocalPassiveMode(); ftpClient.changeWorkingDirectory("/" + directory);// 转移到FTP服务器目录 FTPFile[] ftpFiles = ftpClient.listFiles(); if (ftpFiles != null && ftpFiles.length > 0) { ArrayList list = new ArrayList(Arrays.asList(ftpFiles)); Collections.sort(list, new Comparator() { @Override public int compare(FTPFile o1, FTPFile o2) { long time = o1.getTimestamp().getTime().getTime(); long time1 = o2.getTimestamp().getTime().getTime(); if (time > time1) { return 1; } if (time == time1) { return 0; } return -1; } }); for (FTPFile file : list) { if (file.getName().toUpperCase().endsWith("." + fileType)) { File localFile = new File(System.getProperty("java.io.tmpdir") + File.separator + file.getName()); OutputStream os = new FileOutputStream(localFile); ftpClient.retrieveFile(file.getName(), os); os.close(); files.add(localFile); } } } } catch (Exception e) { BaseUtil.getLogger().error(e.toString()); e.printStackTrace(); } return files; } /** * * @param ftpClient * @return 获取连接到的ftp站点下的文件夹所有文件 */ public static List downloadFileByDirectory(FTPClient ftpClient,String directory) { List files = new ArrayList(); try { ftpClient.enterLocalPassiveMode(); // 转移到FTP服务器目录 ftpClient.changeWorkingDirectory("/" + directory); FTPFile[] ftpFiles = ftpClient.listFiles(); if (ftpFiles != null && ftpFiles.length > 0) { ArrayList list = new ArrayList(Arrays.asList(ftpFiles)); Collections.sort(list, new Comparator() { @Override public int compare(FTPFile o1, FTPFile o2) { long time = o1.getTimestamp().getTime().getTime(); long time1 = o2.getTimestamp().getTime().getTime(); if (time > time1) { return 1; } if (time == time1) { return 0; } return -1; } }); for (FTPFile file : list) { if(!".".equals(file.getName()) && !"..".equals(file.getName())&& !"bak".equals(file.getName())&& !"failed".equals(file.getName())) { File localFile = new File(System.getProperty("java.io.tmpdir") + File.separator + file.getName()); OutputStream os = new FileOutputStream(localFile); ftpClient.retrieveFile(file.getName(), os); os.close(); files.add(localFile); } } } } catch (Exception e) { BaseUtil.getLogger().error(e.toString()); e.printStackTrace(); } return files; } }