| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- 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<String,Object> map) throws Exception {
- return connect(map,map.get("downloadpath").toString());
- }
- /**
- * 获取ftp连接,该连接指向ftp配置中的downloadpath文件夹
- * @param map 包含ftp连接配置的map
- */
- public static FTPClient connect(Map<String,Object> 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<File> downloadAllFileByType(FTPClient ftpClient,String fileType) {
- List<File> files = new ArrayList<File>();
- try {
- ftpClient.enterLocalPassiveMode();
- FTPFile[] ftpFiles = ftpClient.listFiles();
- if (ftpFiles != null && ftpFiles.length > 0) {
- ArrayList<FTPFile> list = new ArrayList<FTPFile>(Arrays.asList(ftpFiles));
- Collections.sort(list, new Comparator<FTPFile>() {
- @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<File> downloadAllFileByType(FTPClient ftpClient) {
- List<File> files = new ArrayList<File>();
- 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<File> downloadFileByDirectory(FTPClient ftpClient,String directory,String fileType) {
- List<File> files = new ArrayList<File>();
- try {
- ftpClient.enterLocalPassiveMode();
- ftpClient.changeWorkingDirectory("/" + directory);// 转移到FTP服务器目录
- FTPFile[] ftpFiles = ftpClient.listFiles();
- if (ftpFiles != null && ftpFiles.length > 0) {
- ArrayList<FTPFile> list = new ArrayList<FTPFile>(Arrays.asList(ftpFiles));
- Collections.sort(list, new Comparator<FTPFile>() {
- @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<File> downloadFileByDirectory(FTPClient ftpClient,String directory) {
- List<File> files = new ArrayList<File>();
- try {
- ftpClient.enterLocalPassiveMode();
- // 转移到FTP服务器目录
- ftpClient.changeWorkingDirectory("/" + directory);
- FTPFile[] ftpFiles = ftpClient.listFiles();
- if (ftpFiles != null && ftpFiles.length > 0) {
- ArrayList<FTPFile> list = new ArrayList<FTPFile>(Arrays.asList(ftpFiles));
- Collections.sort(list, new Comparator<FTPFile>() {
- @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;
- }
- }
|