| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- package com.uas.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 java.util.Map;
- import java.util.Properties;
- import com.jcraft.jsch.JSchException;
- 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站点,判断是否可以连接
- * @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.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){
- e.printStackTrace();
- BaseUtil.getLogger().error("ip:" + addr + e.toString());
- return null;
- }
- }
- /**
- * 上传文件
- * @param ftpClient 已连接的ftp客户端
- * @param pathname 路径
- * @param file 待上传的文件
- * @return 上传结果
- */
- public static boolean uploadFile(FTPClient ftpClient, String pathname, File file,String name) {
- 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(name, 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();
- for (FTPFile file : ftpFiles) {
- 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;
- }
-
- /**
- * 关闭ftp连接
- * @param ftpClient
- */
- public static void closeFtpClient(FTPClient ftpClient){
- if (ftpClient.isConnected()) {
- try {
- ftpClient.logout();
- } catch (IOException e) {
- BaseUtil.getLogger().error(e.toString());
- }
- }
- }
-
- }
|