ftp上传下载工具类

- 日理万妓 2022-07-13 13:07 340阅读 0赞
  1. /**
  2. * ftp上传下载工具类
  3. * @Title: FtpUtil
  4. * @author xwp
  5. * @date 2017年1月10日下午4:14:59
  6. */
  7. public class FtpUtil {
  8. /**
  9. * Description: 向FTP服务器上传文件
  10. * @param host FTP服务器hostname
  11. * @param port FTP服务器端口
  12. * @param username FTP登录账号
  13. * @param password FTP登录密码
  14. * @param basePath FTP服务器基础目录
  15. * @param filePath FTP服务器文件存放路径。例如分日期存放:/2017/01/10。文件的路径为basePath+filePath
  16. * @param filename 上传到FTP服务器上的文件名
  17. * @param input 输入流
  18. * @return 成功返回true,否则返回false
  19. */
  20. public static boolean uploadFile(String host, int port, String username, String password, String basePath,
  21. String filePath, String filename, InputStream input) {
  22. boolean result = false;
  23. FTPClient ftp = new FTPClient();
  24. try {
  25. int reply;
  26. ftp.connect(host, port);// 连接FTP服务器 ("192.168.0.128" , "21") ftp默认端口为21端口
  27. // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
  28. ftp.login(username, password);// 登录
  29. reply = ftp.getReplyCode();
  30. if (!FTPReply.isPositiveCompletion(reply)) {
  31. ftp.disconnect();
  32. return result;
  33. }
  34. //切换到上传目录 changeWorkingDirectory定义传输文件路径(绝对路径+相对路径)
  35. if (!ftp.changeWorkingDirectory(basePath+filePath)) {
  36. //如果目录不存在创建目录
  37. String[] dirs = filePath.split("/");
  38. String tempPath = basePath;
  39. for (String dir : dirs) {
  40. if (null == dir || "".equals(dir)) continue;
  41. tempPath += "/" + dir;
  42. if (!ftp.changeWorkingDirectory(tempPath)) {
  43. if (!ftp.makeDirectory(tempPath)) {
  44. return result;
  45. } else {
  46. ftp.changeWorkingDirectory(tempPath);
  47. }
  48. }
  49. }
  50. }
  51. //设置上传文件的类型为二进制类型
  52. ftp.setFileType(FTP.BINARY_FILE_TYPE);
  53. //上传文件 storeFile定义文件名和传入参数
  54. if (!ftp.storeFile(filename, input)) {
  55. return result;
  56. }
  57. input.close();
  58. ftp.logout();
  59. result = true;
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. } finally {
  63. if (ftp.isConnected()) {
  64. try {
  65. ftp.disconnect();
  66. } catch (IOException ioe) {
  67. }
  68. }
  69. }
  70. return result;
  71. }
  72. /**
  73. * Description: 从FTP服务器下载文件
  74. * @param host FTP服务器hostname
  75. * @param port FTP服务器端口
  76. * @param username FTP登录账号
  77. * @param password FTP登录密码
  78. * @param remotePath FTP服务器上的相对路径
  79. * @param fileName 要下载的文件名
  80. * @param localPath 下载后保存到本地的路径
  81. * @return
  82. */
  83. public static boolean downloadFile(String host, int port, String username, String password, String remotePath,
  84. String fileName, String localPath) {
  85. boolean result = false;
  86. FTPClient ftp = new FTPClient();
  87. try {
  88. int reply;
  89. ftp.connect(host, port);
  90. // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
  91. ftp.login(username, password);// 登录
  92. reply = ftp.getReplyCode();
  93. if (!FTPReply.isPositiveCompletion(reply)) {
  94. ftp.disconnect();
  95. return result;
  96. }
  97. ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
  98. FTPFile[] fs = ftp.listFiles();
  99. for (FTPFile ff : fs) {
  100. if (ff.getName().equals(fileName)) {
  101. File localFile = new File(localPath + "/" + ff.getName());
  102. OutputStream is = new FileOutputStream(localFile);
  103. ftp.retrieveFile(ff.getName(), is);
  104. is.close();
  105. }
  106. }
  107. ftp.logout();
  108. result = true;
  109. } catch (IOException e) {
  110. e.printStackTrace();
  111. } finally {
  112. if (ftp.isConnected()) {
  113. try {
  114. ftp.disconnect();
  115. } catch (IOException ioe) {
  116. }
  117. }
  118. }
  119. return result;
  120. }
  121. public static void main(String[] args) {
  122. try {
  123. FileInputStream in=new FileInputStream(new File("D:\\images\\gaigeming.jpg"));
  124. boolean flag = uploadFile("192.168.58.131", 21, "ftpuser", "ftpuser", "/home/ftpuser/www/images","/2017/01/10", "hello.jpg", in);
  125. System.out.println(flag);
  126. } catch (FileNotFoundException e) {
  127. e.printStackTrace();
  128. }
  129. }
  130. }

发表评论

表情:
评论列表 (有 0 条评论,340人围观)

还没有评论,来说两句吧...

相关阅读