FtpUtil.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package com.uas.util;
  2. import com.jcraft.jsch.JSchException;
  3. import org.apache.commons.net.ftp.FTPClient;
  4. import org.apache.commons.net.ftp.FTPFile;
  5. import org.apache.commons.net.ftp.FTPReply;
  6. import java.io.*;
  7. import java.util.*;
  8. public class FtpUtil {
  9. /**
  10. * 连接到ftp站点,判断是否可以连接
  11. * @param path 要连接到的目录
  12. * @param addr 站点地址
  13. * @param port 端口
  14. * @param username 用户名
  15. * @param password 密码
  16. * @return
  17. * @throws Exception
  18. */
  19. public static boolean connect(String path, String addr, int port, String username,
  20. String password) {
  21. boolean result = false;
  22. try {
  23. FTPClient ftp = new FTPClient();
  24. int reply;
  25. ftp.connect(addr, port);
  26. ftp.login(username, password);
  27. ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
  28. reply = ftp.getReplyCode();
  29. if (!FTPReply.isPositiveCompletion(reply)) {
  30. ftp.disconnect();
  31. return result;
  32. }
  33. ftp.changeWorkingDirectory(path);
  34. result = true;
  35. return result;
  36. }catch (Exception e){
  37. SFTPUtil sftp = new SFTPUtil(username, password, addr, port);
  38. try {
  39. sftp.login();
  40. result = true;
  41. } catch (JSchException e1) {
  42. e1.printStackTrace();
  43. }
  44. return result;
  45. }
  46. }
  47. /**
  48. * 获取ftp连接,该连接指向ftp配置中的downloadpath文件夹
  49. * @param map 包含ftp连接配置的map
  50. */
  51. public static FTPClient connect(Map<String,Object> map) throws Exception {
  52. return connect(map,map.get("downloadpath").toString());
  53. }
  54. /**
  55. * 获取ftp连接,该连接指向ftp配置中的downloadpath文件夹
  56. * @param map 包含ftp连接配置的map
  57. */
  58. public static FTPClient connect(Map<String,Object> map,String path){
  59. FTPClient ftp = null;
  60. String addr = null;
  61. try{
  62. addr = map.get("ip").toString();
  63. int port = Integer.parseInt(map.get("port").toString());
  64. String username = map.get("user").toString();
  65. String password = map.get("password").toString();
  66. ftp = new FTPClient();
  67. int reply;
  68. ftp.connect(addr, port);
  69. if(!ftp.login(username, password)){
  70. return null;
  71. }
  72. ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
  73. reply = ftp.getReplyCode();
  74. if (!FTPReply.isPositiveCompletion(reply)) {
  75. ftp.disconnect();
  76. return null;
  77. }
  78. ftp.changeWorkingDirectory(path);
  79. return ftp;
  80. }catch(Exception e){
  81. e.printStackTrace();
  82. BaseUtil.getLogger().error("ip:" + addr + e.toString());
  83. return null;
  84. }
  85. }
  86. /**
  87. * 上传文件
  88. * @param ftpClient 已连接的ftp客户端
  89. * @param pathname 路径
  90. * @param file 待上传的文件
  91. * @return 上传结果
  92. */
  93. public static boolean uploadFile(FTPClient ftpClient, String pathname, File file) {
  94. boolean flag = false;
  95. ftpClient.setControlEncoding("UTF-8");
  96. try {
  97. ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
  98. boolean change = ftpClient.changeWorkingDirectory("/"+pathname);
  99. if(!change){
  100. BaseUtil.getLogger().info("change to " + pathname + " fail");
  101. return false;
  102. }
  103. InputStream inputStream = new FileInputStream(file);
  104. ftpClient.enterLocalPassiveMode();
  105. ftpClient.storeFile(file.getName(), inputStream);
  106. inputStream.close();
  107. //ftpClient.logout();
  108. flag = true;
  109. } catch (Exception e) {
  110. e.printStackTrace();
  111. BaseUtil.getLogger().error(e.toString());
  112. if (ftpClient.isConnected()) {
  113. try {
  114. ftpClient.disconnect();
  115. } catch (IOException e1) {
  116. BaseUtil.getLogger().error(e1.toString());
  117. e1.printStackTrace();
  118. }
  119. }
  120. }
  121. return flag;
  122. }
  123. /**
  124. * 删除后关闭ftp连接
  125. * @param ftpClient
  126. * @param filename 要删除的文件名
  127. * @return 删除结果
  128. */
  129. public static boolean deleteFile(FTPClient ftpClient,String filename) {
  130. boolean flag = false;
  131. try {
  132. ftpClient.dele(filename);
  133. flag = true;
  134. } catch (Exception e) {
  135. BaseUtil.getLogger().error(e.toString());
  136. e.printStackTrace();
  137. }
  138. return flag;
  139. }
  140. /**
  141. *
  142. * @param ftpClient
  143. * fileType 需要下载的文件类型
  144. * @return 获取连接到的ftp站点下的文件夹所有文件
  145. */
  146. public static List<File> downloadAllFileByType(FTPClient ftpClient,String fileType) {
  147. List<File> files = new ArrayList<File>();
  148. try {
  149. ftpClient.enterLocalPassiveMode();
  150. FTPFile[] ftpFiles = ftpClient.listFiles();
  151. if (ftpFiles != null && ftpFiles.length > 0) {
  152. ArrayList<FTPFile> list = new ArrayList<FTPFile>(Arrays.asList(ftpFiles)) ;
  153. Collections.sort(list, new Comparator<FTPFile>() {
  154. @Override
  155. public int compare(FTPFile o1, FTPFile o2) {
  156. long time = o1.getTimestamp().getTime().getTime();
  157. long time1 = o2.getTimestamp().getTime().getTime();
  158. if ( time>time1 ) {
  159. return 1;
  160. }
  161. if (time == time1) {
  162. return 0;
  163. }
  164. return -1;
  165. }
  166. });
  167. for (FTPFile file : list) {
  168. if(file.getName().toUpperCase().endsWith("." + fileType)){
  169. File localFile = new File(System.getProperty("java.io.tmpdir") + File.separator + file.getName());
  170. OutputStream os = new FileOutputStream(localFile);
  171. ftpClient.retrieveFile(file.getName(), os);
  172. os.close();
  173. files.add(localFile);
  174. }
  175. }
  176. }
  177. } catch (Exception e) {
  178. BaseUtil.getLogger().error(e.toString());
  179. e.printStackTrace();
  180. }
  181. return files;
  182. }
  183. /**
  184. *
  185. * @param ftpClient
  186. * fileType 需要下载的文件类型
  187. * @return 获取连接到的ftp站点下的文件夹所有文件
  188. */
  189. public static List<File> downloadAllFileByDir(FTPClient ftpClient,String dir) {
  190. List<File> files = new ArrayList<File>();
  191. try {
  192. ftpClient.enterLocalPassiveMode();
  193. ftpClient.changeWorkingDirectory("/"+dir+"/");
  194. FTPFile[] ftpFiles = ftpClient.listFiles("/"+dir+"/");
  195. for (FTPFile file : ftpFiles) {
  196. if(!".".equals(file.getName()) && !"..".equals(file.getName())&& !"bak".equals(file.getName())){
  197. File localFile = new File(System.getProperty("java.io.tmpdir") + File.separator + file.getName());
  198. OutputStream os = new FileOutputStream(localFile);
  199. ftpClient.retrieveFile(file.getName(), os);
  200. os.close();
  201. files.add(localFile);
  202. }
  203. }
  204. } catch (Exception e) {
  205. BaseUtil.getLogger().error(e.toString());
  206. e.printStackTrace();
  207. }
  208. return files;
  209. }
  210. /**
  211. *
  212. * @param ftpClient
  213. * fileType 需要下载的文件类型
  214. * @return 获取连接到的ftp站点下的指定文件夹所有文件
  215. */
  216. public static List<File> downloadDirFileByType(FTPClient ftpClient,String fileType,String dir) {
  217. List<File> files = new ArrayList<File>();
  218. try {
  219. ftpClient.enterLocalPassiveMode();
  220. ftpClient.changeWorkingDirectory("/"+dir+"/");
  221. FTPFile[] ftpFiles = ftpClient.listFiles("/"+dir+"/");
  222. if (ftpFiles != null && ftpFiles.length > 0) {
  223. ArrayList<FTPFile> list = new ArrayList<FTPFile>(Arrays.asList(ftpFiles));
  224. Collections.sort(list, new Comparator<FTPFile>() {
  225. @Override
  226. public int compare(FTPFile o1, FTPFile o2) {
  227. long time = o1.getTimestamp().getTime().getTime();
  228. long time1 = o2.getTimestamp().getTime().getTime();
  229. if (time > time1) {
  230. return 1;
  231. }
  232. if (time == time1) {
  233. return 0;
  234. }
  235. return -1;
  236. }
  237. });
  238. for (FTPFile file : list) {
  239. if (file.getName().toUpperCase().endsWith("." + fileType)) {
  240. File localFile = new File(System.getProperty("java.io.tmpdir") + File.separator + file.getName());
  241. OutputStream os = new FileOutputStream(localFile);
  242. ftpClient.retrieveFile(file.getName(), os);
  243. os.close();
  244. files.add(localFile);
  245. }
  246. }
  247. }
  248. } catch (Exception e) {
  249. BaseUtil.getLogger().error(e.toString());
  250. e.printStackTrace();
  251. }
  252. return files;
  253. }
  254. /**
  255. * 关闭ftp连接
  256. * @param ftpClient
  257. */
  258. public static void closeFtpClient(FTPClient ftpClient){
  259. if (ftpClient.isConnected()) {
  260. try {
  261. ftpClient.logout();
  262. } catch (IOException e) {
  263. BaseUtil.getLogger().error(e.toString());
  264. }
  265. }
  266. }
  267. }