FtpUtil.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package com.xt.jygl.ftp;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStream;
  7. import org.apache.commons.net.ftp.FTPClient;
  8. import org.apache.commons.net.ftp.FTPFile;
  9. import org.apache.commons.net.ftp.FTPReply;
  10. import org.apache.log4j.Logger;
  11. import com.xt.jygl.common.GlobalData;
  12. public class FtpUtil {
  13. private static Logger logger = Logger.getLogger(FtpUtil.class);
  14. private static FTPClient ftp;
  15. /**
  16. * 获取ftp连接
  17. *
  18. * @param f
  19. * @return
  20. * @throws Exception
  21. */
  22. public static boolean connectFtp(Ftp f) throws Exception {
  23. boolean flag = false;
  24. try {
  25. ftp = new FTPClient();
  26. ftp.connect(f.getIpAddr(), f.getPort());
  27. ftp.login(f.getUserName(), f.getPwd());
  28. ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
  29. int reply = ftp.getReplyCode();
  30. if (!FTPReply.isPositiveCompletion(reply)) {
  31. ftp.disconnect();
  32. return flag;
  33. }
  34. ftp.changeWorkingDirectory(f.getPath());
  35. flag = true;
  36. } catch (Exception e) {
  37. flag = false;
  38. logger.error(e);
  39. logger.error("FTP链接失败");
  40. }
  41. return flag;
  42. }
  43. /**
  44. * 关闭ftp连接
  45. */
  46. public static void closeFtp() {
  47. if (ftp != null && ftp.isConnected()) {
  48. try {
  49. ftp.logout();
  50. ftp.disconnect();
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. }
  54. }
  55. }
  56. /**
  57. * ftp上传文件
  58. *
  59. * @param f
  60. * @throws Exception
  61. */
  62. @SuppressWarnings("finally")
  63. public static boolean upload(File file) {
  64. boolean flag = true;
  65. try {
  66. if (file.isDirectory()) {
  67. ftp.makeDirectory(new String(file.getName().getBytes("UTF-8"), "iso-8859-1"));
  68. ftp.changeWorkingDirectory(file.getName());
  69. String[] files = file.list();
  70. for (int i = 0; i < files.length; i++) {
  71. File file1 = new File(file.getPath() + "\\" + files[i]);
  72. if (file1.isDirectory()) {
  73. upload(file1);
  74. ftp.changeToParentDirectory();
  75. } else {
  76. File file2 = new File(file.getPath() + "\\" + files[i]);
  77. FileInputStream input = new FileInputStream(file2);
  78. ftp.storeFile(file2.getName(), input);
  79. input.close();
  80. }
  81. }
  82. } else {
  83. FileInputStream input = new FileInputStream(file);
  84. ftp.setControlEncoding("UTF-8");
  85. ftp.storeFile(new String(file.getName().getBytes("UTF-8"), "iso-8859-1"), input);
  86. input.close();
  87. }
  88. } catch (Exception e) {
  89. flag = false;
  90. e.printStackTrace();
  91. } finally {
  92. closeFtp();
  93. return flag;
  94. }
  95. }
  96. /**
  97. * 下载链接配置
  98. *
  99. * @param f
  100. * @param localBaseDir
  101. * 本地目录
  102. * @param remoteBaseDir
  103. * 远程目录
  104. * @throws Exception
  105. */
  106. public static boolean startDown(Ftp f, String localBaseDir, String remoteBaseDir, String filename) throws Exception {
  107. boolean flag = false;
  108. try {
  109. if (FtpUtil.connectFtp(f)) {
  110. FTPFile[] files = null;
  111. boolean changedir = ftp.changeWorkingDirectory(remoteBaseDir);
  112. if (changedir) {
  113. ftp.setControlEncoding("GBK");
  114. files = ftp.listFiles();
  115. for (int i = 0; i < files.length; i++) {
  116. if (filename.equals(files[i].getName())) {
  117. try {
  118. flag = downloadFile(files[i], localBaseDir, remoteBaseDir);
  119. } catch (Exception e) {
  120. logger.error(e);
  121. logger.error("<" + files[i].getName() + ">下载失败");
  122. }
  123. }
  124. }
  125. }
  126. } else {
  127. logger.error("链接失败!");
  128. flag = false;
  129. }
  130. } catch (Exception e) {
  131. flag = false;
  132. logger.error(e);
  133. logger.error("FTP下载过程中链接失败");
  134. } finally {
  135. closeFtp();
  136. }
  137. return flag;
  138. }
  139. /**
  140. *
  141. * 下载FTP文件 当你需要下载FTP文件的时候,调用此方法 根据<b>获取的文件名,本地地址,远程地址</b>进行下载
  142. *
  143. * @param ftpFile
  144. * @param relativeLocalPath
  145. * @param relativeRemotePath
  146. */
  147. private static boolean downloadFile(FTPFile ftpFile, String relativeLocalPath, String relativeRemotePath) {
  148. boolean flag = false;
  149. if (ftpFile.isFile()) {
  150. if (ftpFile.getName().indexOf("?") == -1) {
  151. OutputStream outputStream = null;
  152. try {
  153. File locaFile = new File(relativeLocalPath + ftpFile.getName());
  154. // 判断文件是否存在,存在则返回
  155. if (locaFile.exists()) {
  156. return true;
  157. } else {
  158. outputStream = new FileOutputStream(relativeLocalPath + ftpFile.getName());
  159. ftp.retrieveFile(ftpFile.getName(), outputStream);
  160. outputStream.flush();
  161. outputStream.close();
  162. flag = true;
  163. }
  164. } catch (Exception e) {
  165. logger.error(e);
  166. } finally {
  167. try {
  168. if (outputStream != null) {
  169. outputStream.close();
  170. }
  171. } catch (IOException e) {
  172. logger.error("输出文件流异常");
  173. }
  174. }
  175. }
  176. } else {
  177. String newlocalRelatePath = relativeLocalPath + ftpFile.getName();
  178. String newRemote = new String(relativeRemotePath + ftpFile.getName().toString());
  179. File fl = new File(newlocalRelatePath);
  180. if (!fl.exists()) {
  181. fl.mkdirs();
  182. }
  183. try {
  184. newlocalRelatePath = newlocalRelatePath + '/';
  185. newRemote = newRemote + "/";
  186. String currentWorkDir = ftpFile.getName().toString();
  187. boolean changedir = ftp.changeWorkingDirectory(currentWorkDir);
  188. if (changedir) {
  189. FTPFile[] files = null;
  190. files = ftp.listFiles();
  191. for (int i = 0; i < files.length; i++) {
  192. downloadFile(files[i], newlocalRelatePath, newRemote);
  193. }
  194. }
  195. if (changedir) {
  196. ftp.changeToParentDirectory();
  197. }
  198. } catch (Exception e) {
  199. logger.error(e);
  200. }
  201. }
  202. return flag;
  203. }
  204. public static void main(String[] args) throws Exception {
  205. Ftp f = new Ftp();
  206. f.setIpAddr("192.168.30.145");
  207. f.setPort(21);
  208. f.setUserName("test");
  209. f.setPwd("test");
  210. f.setPath(GlobalData.FTP_VIEW_PATH);
  211. FtpUtil.connectFtp(f);
  212. String path = "D:\\upload\\xkzsq";
  213. File file = new File(path);
  214. FtpUtil.upload(file);// 把文件上传在ftp上
  215. // FtpUtil.startDown(f, "E:\\upload\\jdtx\\", "/jdtx/",
  216. // "20160718164041.txt");// 下载ftp文件测试
  217. System.out.println("ok");
  218. }
  219. public static void makeDirFile(String path) {
  220. File file = new File(path);
  221. if (file.isDirectory()) {
  222. try {
  223. ftp.makeDirectory(new String(file.getName().getBytes("UTF-8"), "iso-8859-1"));
  224. ftp.changeWorkingDirectory(file.getName());
  225. } catch (Exception e) {
  226. e.printStackTrace();
  227. }
  228. }
  229. }
  230. }