FtpUtils.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. package com.goafanti.common.utils;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.net.MalformedURLException;
  9. import org.apache.commons.net.ftp.FTPClient;
  10. import org.apache.commons.net.ftp.FTPFile;
  11. import org.apache.commons.net.ftp.FTPReply;
  12. public class FtpUtils {
  13. //ftp服务器地址
  14. public static final String HOSTNAME = "118.190.205.7";
  15. //ftp服务器端口号默认为21
  16. public static final Integer PORT = 21 ;
  17. //ftp登录账号
  18. public static final String USERNAME = "video";
  19. //ftp登录密码
  20. public static final String PASSWORD = "aft2018";
  21. public FTPClient ftpClient = null;
  22. /**
  23. * 初始化ftp服务器
  24. */
  25. public void initFtpClient() {
  26. ftpClient = new FTPClient();
  27. ftpClient.setControlEncoding("utf-8");
  28. try {
  29. System.out.println("connecting...ftp服务器:"+ HOSTNAME+":"+ PORT);
  30. ftpClient.connect(HOSTNAME, PORT); //连接ftp服务器
  31. ftpClient.login(USERNAME, PASSWORD); //登录ftp服务器
  32. int replyCode = ftpClient.getReplyCode(); //是否成功登录服务器
  33. if(!FTPReply.isPositiveCompletion(replyCode)){
  34. System.out.println("connect failed...ftp服务器:"+ HOSTNAME+":"+ PORT);
  35. }
  36. System.out.println("connect successfu...ftp服务器:"+ HOSTNAME+":"+ PORT);
  37. }catch (MalformedURLException e) {
  38. e.printStackTrace();
  39. }catch (IOException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. /**
  44. * 上传文件
  45. * @param pathname ftp服务保存地址
  46. * @param fileName 上传到ftp的文件名
  47. * @param originfilename 待上传文件的名称(绝对地址) *
  48. * @return
  49. */
  50. public boolean uploadFile( String pathname, String fileName,String originfilename){
  51. boolean flag = false;
  52. InputStream inputStream = null;
  53. try{
  54. System.out.println("开始上传文件");
  55. inputStream = new FileInputStream(new File(originfilename));
  56. System.out.println(inputStream==null?"null":"notnull");
  57. initFtpClient();
  58. ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
  59. CreateDirecroty(pathname);
  60. System.out.println("11111ftp服务保存地址"+pathname);
  61. System.out.println("22222上传到ftp的文件名"+fileName);
  62. System.out.println("33333待上传文件的名称"+originfilename);
  63. System.out.println("44444创建指定文件夹");
  64. System.out.println("55555===" + ftpClient.makeDirectory(pathname));
  65. System.out.println("66666/*/*" + ftpClient.changeWorkingDirectory(pathname));
  66. ftpClient.enterLocalPassiveMode();
  67. System.out.println("77777#####"+ftpClient.storeFile(fileName, inputStream));
  68. inputStream.close();
  69. ftpClient.logout();
  70. System.out.println("888888注销ftp");
  71. flag = true;
  72. System.out.println("上传文件成功");
  73. }catch (Exception e) {
  74. System.out.println("上传文件失败");
  75. e.printStackTrace();
  76. }finally{
  77. System.out.println("finally处理");
  78. if(ftpClient.isConnected()){
  79. try{
  80. ftpClient.disconnect();
  81. }catch(IOException e){
  82. e.printStackTrace();
  83. }
  84. }
  85. if(null != inputStream){
  86. try {
  87. inputStream.close();
  88. } catch (IOException e) {
  89. e.printStackTrace();
  90. }
  91. }
  92. }
  93. return true;
  94. }
  95. /**
  96. * 上传文件
  97. * @param pathname ftp服务保存地址
  98. * @param fileName 上传到ftp的文件名
  99. * @param inputStream 输入文件流
  100. * @return
  101. */
  102. public boolean uploadFile( String pathname, String fileName,InputStream inputStream){
  103. boolean flag = false;
  104. try{
  105. System.out.println("开始上传文件");
  106. initFtpClient();
  107. ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
  108. CreateDirecroty(pathname);
  109. ftpClient.makeDirectory(pathname);
  110. ftpClient.changeWorkingDirectory(pathname);
  111. ftpClient.storeFile(fileName, inputStream);
  112. inputStream.close();
  113. ftpClient.logout();
  114. flag = true;
  115. System.out.println("上传文件成功");
  116. }catch (Exception e) {
  117. System.out.println("上传文件失败");
  118. e.printStackTrace();
  119. }finally{
  120. if(ftpClient.isConnected()){
  121. try{
  122. ftpClient.disconnect();
  123. }catch(IOException e){
  124. e.printStackTrace();
  125. }
  126. }
  127. if(null != inputStream){
  128. try {
  129. inputStream.close();
  130. } catch (IOException e) {
  131. e.printStackTrace();
  132. }
  133. }
  134. }
  135. return true;
  136. }
  137. //改变目录路径
  138. public boolean changeWorkingDirectory(String directory) {
  139. boolean flag = true;
  140. try {
  141. flag = ftpClient.changeWorkingDirectory(directory);
  142. if (flag) {
  143. System.out.println("进入文件夹" + directory + " 成功!");
  144. } else {
  145. System.out.println("进入文件夹" + directory + " 失败!开始创建文件夹");
  146. }
  147. } catch (IOException ioe) {
  148. ioe.printStackTrace();
  149. }
  150. return flag;
  151. }
  152. //创建多层目录文件,如果有ftp服务器已存在该文件,则不创建,如果无,则创建
  153. public boolean CreateDirecroty(String remote) throws IOException {
  154. boolean success = true;
  155. String directory = remote + "/";
  156. // 如果远程目录不存在,则递归创建远程服务器目录
  157. if (!directory.equalsIgnoreCase("/") && !changeWorkingDirectory(new String(directory))) {
  158. int start = 0;
  159. int end = 0;
  160. if (directory.startsWith("/")) {
  161. start = 1;
  162. } else {
  163. start = 0;
  164. }
  165. end = directory.indexOf("/", start);
  166. String path = "";
  167. String paths = "";
  168. while (true) {
  169. String subDirectory = new String(remote.substring(start, end).getBytes("GBK"), "iso-8859-1");
  170. path = path + "/" + subDirectory;
  171. if (!existFile(path)) {
  172. if (makeDirectory(subDirectory)) {
  173. changeWorkingDirectory(subDirectory);
  174. } else {
  175. System.out.println("创建目录[" + subDirectory + "]失败");
  176. changeWorkingDirectory(subDirectory);
  177. }
  178. } else {
  179. changeWorkingDirectory(subDirectory);
  180. }
  181. paths = paths + "/" + subDirectory;
  182. start = end + 1;
  183. end = directory.indexOf("/", start);
  184. // 检查所有目录是否创建完毕
  185. if (end <= start) {
  186. break;
  187. }
  188. }
  189. }
  190. return success;
  191. }
  192. //判断ftp服务器文件是否存在
  193. public boolean existFile(String path) throws IOException {
  194. boolean flag = false;
  195. FTPFile[] ftpFileArr = ftpClient.listFiles(path);
  196. if (ftpFileArr.length > 0) {
  197. flag = true;
  198. }
  199. return flag;
  200. }
  201. //创建目录
  202. public boolean makeDirectory(String dir) {
  203. boolean flag = true;
  204. try {
  205. flag = ftpClient.makeDirectory(dir);
  206. if (flag) {
  207. System.out.println("创建文件夹" + dir + " 成功!");
  208. } else {
  209. System.out.println("创建文件夹" + dir + " 失败!");
  210. }
  211. } catch (Exception e) {
  212. e.printStackTrace();
  213. }
  214. return flag;
  215. }
  216. /** * 下载文件 *
  217. * @param pathname FTP服务器文件目录 *
  218. * @param filename 文件名称 *
  219. * @param localpath 下载后的文件路径 *
  220. * @return */
  221. public boolean downloadFile(String pathname, String filename, String localpath){
  222. boolean flag = false;
  223. OutputStream os=null;
  224. try {
  225. System.out.println("开始下载文件");
  226. initFtpClient();
  227. //切换FTP目录
  228. ftpClient.changeWorkingDirectory(pathname);
  229. FTPFile[] ftpFiles = ftpClient.listFiles();
  230. for(FTPFile file : ftpFiles){
  231. if(filename.equalsIgnoreCase(file.getName())){
  232. File localFile = new File(localpath + "/" + file.getName());
  233. os = new FileOutputStream(localFile);
  234. ftpClient.retrieveFile(file.getName(), os);
  235. os.close();
  236. }
  237. }
  238. ftpClient.logout();
  239. flag = true;
  240. System.out.println("下载文件成功");
  241. } catch (Exception e) {
  242. System.out.println("下载文件失败");
  243. e.printStackTrace();
  244. } finally{
  245. if(ftpClient.isConnected()){
  246. try{
  247. ftpClient.disconnect();
  248. }catch(IOException e){
  249. e.printStackTrace();
  250. }
  251. }
  252. if(null != os){
  253. try {
  254. os.close();
  255. } catch (IOException e) {
  256. e.printStackTrace();
  257. }
  258. }
  259. }
  260. return flag;
  261. }
  262. /** * 删除文件 *
  263. * @param pathname FTP服务器保存目录 *
  264. * @param filename 要删除的文件名称 *
  265. * @return */
  266. public boolean deleteFile(String pathname, String filename){
  267. boolean flag = false;
  268. try {
  269. System.out.println("开始删除文件");
  270. initFtpClient();
  271. //切换FTP目录
  272. ftpClient.changeWorkingDirectory(pathname);
  273. ftpClient.dele(filename);
  274. ftpClient.logout();
  275. flag = true;
  276. System.out.println("删除文件成功");
  277. } catch (Exception e) {
  278. System.out.println("删除文件失败");
  279. e.printStackTrace();
  280. } finally {
  281. if(ftpClient.isConnected()){
  282. try{
  283. ftpClient.disconnect();
  284. }catch(IOException e){
  285. e.printStackTrace();
  286. }
  287. }
  288. }
  289. return flag;
  290. }
  291. public static void main(String[] args) {
  292. FtpUtils ftp =new FtpUtils();
  293. ftp.uploadFile("ftpFile/data", "aaa.avi", "F:\\BaiduNetdiskDownload\\day63_project_OA_day12_JBPM04\\video\\aaa.avi");
  294. System.out.println("ok");
  295. }
  296. }