ContinueFTPService.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package com.jiayue.ipfcst.ftpsftp.service;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.apache.commons.net.ftp.FTPClient;
  4. import org.apache.commons.net.ftp.FTPFile;
  5. import org.springframework.stereotype.Service;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.nio.charset.StandardCharsets;
  9. @Service
  10. @Slf4j
  11. public class ContinueFTPService {
  12. public FTPClient client;
  13. /**
  14. * 登录
  15. *
  16. * @param host ip地址
  17. * @param port 远程端口
  18. * @param userName 用户名
  19. * @param password 密码
  20. * @throws IOException 抛出异常
  21. */
  22. public void initFtpClient(String host, int port, String userName,
  23. String password) throws IOException {
  24. client = new FTPClient();//s生成一个新的client
  25. client.connect(host, port);//链接
  26. client.login(userName, password);//通过用户名和密码登录
  27. }
  28. /**
  29. * 得到所有目录
  30. *
  31. * @param remotepath 远程文件地址
  32. * @return 返回FTPFile[]
  33. */
  34. public FTPFile[] listFiles(String remotepath) throws IOException {
  35. client.enterLocalPassiveMode();
  36. remotepath = new String(remotepath.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
  37. client.changeWorkingDirectory(remotepath);//获取所有的目录
  38. return client.listFiles();//返回获取到的目录信息
  39. }
  40. /**
  41. * 上传
  42. *
  43. * @param remotepath (ftp的路径)
  44. * @return boolean 上传是否成功
  45. * @throws IOException
  46. */
  47. public boolean upload(String remotepath, String filename, InputStream inputStream) throws IOException {
  48. if (null == client) {//如果获取到的信息为空则返回false
  49. return false;
  50. }
  51. client.setFileType(FTPClient.BINARY_FILE_TYPE);//设置上传文件的类型
  52. client.changeWorkingDirectory(remotepath);//获取上传的路径地址
  53. String charSet = System.getProperties().get("file.encoding").toString();
  54. String name = new String(filename.getBytes(charSet), StandardCharsets.ISO_8859_1);
  55. return client.storeFile(name, inputStream);
  56. }
  57. /**
  58. * 下载
  59. *
  60. * @param name 文件名称
  61. * @return 下载是否成功
  62. */
  63. public InputStream downLoad(String name) throws IOException {
  64. InputStream ins = null;
  65. try {
  66. client.setFileType(FTPClient.BINARY_FILE_TYPE);//设置下载文件的类型
  67. //获取系统字符集
  68. String charSet = System.getProperties().get("file.encoding").toString();
  69. String fileName = new String(name.getBytes(charSet), StandardCharsets.ISO_8859_1);
  70. ins = client.retrieveFileStream(fileName);
  71. } catch (Exception e) {
  72. log.error("FTP下载错误");
  73. }
  74. return ins;
  75. }
  76. /**
  77. * 删除文件
  78. *
  79. * @param remotepath ftp服务端路径
  80. * @return boolean
  81. */
  82. public boolean delete(String remotepath, String name) throws IOException {
  83. client.changeWorkingDirectory(remotepath);
  84. String charSet = System.getProperties().get("file.encoding").toString();
  85. String fileName = new String(name.getBytes(charSet), StandardCharsets.ISO_8859_1);
  86. return client.deleteFile(remotepath + fileName);//删除文件是否成功
  87. }
  88. /**
  89. * 创建目录
  90. *
  91. * @param remotepath ftp服务端路径
  92. * @return boolean
  93. */
  94. public boolean makeDirectory(String remotepath) throws IOException {
  95. if (null == client) {
  96. return false;
  97. }
  98. String[] item = remotepath.split("/");//以‘/’分割成字符串数组
  99. String currentPath = "";
  100. for (int i = 0; i < item.length - 1; i++) {
  101. currentPath = currentPath + "/" + item[i];//创建目录
  102. client.makeDirectory(currentPath);
  103. }
  104. return client.makeDirectory(remotepath);
  105. }
  106. /**
  107. * 删除文件
  108. *
  109. * @param remotepath ftp端路径
  110. * @return boolean
  111. */
  112. private boolean deleteDirectory(String remotepath) throws IOException {
  113. FTPFile[] files = listFiles(remotepath);//获取文件数组
  114. for (FTPFile file : files) {
  115. if (file.isDirectory()) { //如果是删除目录
  116. deleteDirectory(remotepath + "/" + file.getName());//删除目录
  117. } else {
  118. client.deleteFile(remotepath + "/" + file.getName());//删除文件
  119. }
  120. }
  121. return client.removeDirectory(remotepath);
  122. }
  123. /**
  124. * 重命名
  125. *
  126. * @param remoteOldPath ftp旧名字文件
  127. * @param remoteNewPath ftp新名字文件
  128. * @return 是否修改名字成功
  129. */
  130. public boolean replaceName(String remoteOldPath, String remoteNewPath) throws IOException {
  131. if (null == client) {
  132. return false;
  133. }
  134. return client.rename(remoteOldPath, remoteNewPath);
  135. }
  136. /**
  137. * 退出登录
  138. *
  139. * @throws IOException 异常
  140. */
  141. public void close() throws IOException {
  142. if (null != client)
  143. client.logout();
  144. }
  145. }