在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Java FTPClient实现文件上传下载

2016-7-29 15:37| 发布者: zhangjf| 查看: 761| 评论: 0

摘要: 在java程序中,经常需要和FTP打交道,比如向FTP服务器上传文件、下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在commons-net包中)实现上传下载文件。 所用到的jar包有: commons-net-1.4.1.jar ...

java程序中,经常需要和FTP打交道,比如向FTP服务器上传文件、下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在commons-net包中)实现上传下载文件。
所用到的jar包有:
commons-net-1.4.1.jar
jakarta-oro.jar
一、上传文件

  1. 文件上传源代码
  2. /**
  3. * Description: 向FTP服务器上传文件
  4. * @Version1.0
  5. * @param url FTP服务器hostname
  6. * @param port FTP服务器端口
  7. * @param username FTP登录账号
  8. * @param password FTP登录密码
  9. * @param path FTP服务器保存目录
  10. * @param filename 上传到FTP服务器上的文件名
  11. * @param input 输入流
  12. * @return 成功返回true,否则返回false
  13. */
  14. public static boolean uploadFile(
  15. String url,//FTP服务器hostname
  16. int port,//FTP服务器端口
  17. String username, // FTP登录账号
  18. String password, //FTP登录密码
  19. String path, //FTP服务器保存目录
  20. String filename, //上传到FTP服务器上的文件名
  21. InputStream input // 输入流
  22. ) {
  23. boolean success = false;
  24. FTPClient ftp = new FTPClient();
  25. try {
  26. int reply;
  27. ftp.connect(url, port);//连接FTP服务器
  28. //如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
  29. ftp.login(username, password);//登录
  30. reply = ftp.getReplyCode();
  31. if (!FTPReply.isPositiveCompletion(reply)) {
  32. ftp.disconnect();
  33. return success;
  34. }
  35. ftp.changeWorkingDirectory(path);
  36. ftp.storeFile(filename, input);
  37. input.close();
  38. ftp.logout();
  39. success = true;
  40. } catch (IOException e) {
  41. e.printStackTrace();
  42. } finally {
  43. if (ftp.isConnected()) {
  44. try {
  45. ftp.disconnect();
  46. } catch (IOException ioe) {
  47. }
  48. }
  49. }
  50. return success;
  51. }
复制代码

以下是文件上传的测试用例:

  1. /**
  2. * 将本地文件上传到FTP服务器上
  3. *
  4. */
  5. public void testUpLoadFromDisk(){
  6. try {
  7. FileInputStream in=new FileInputStream(new File("D:/test.txt"));
  8. boolean flag = uploadFile("127.0.0.1", 21, "administrator", "zyuc2011", "test", "test.txt", in);
  9. System.out.println(flag);
  10. } catch (FileNotFoundException e) {
  11. e.printStackTrace();
  12. }
  13. }
  14. /**
  15. * 在FTP服务器上生成一个文件,并将一个字符串写入到该文件中
  16. *
  17. */
  18. public void testUpLoadFromString(){
  19. try {
  20. String str = "这是要写入的字符串!";
  21. InputStream input = new ByteArrayInputStream(str.getBytes("utf-8"));
  22. boolean flag = uploadFile("127.0.0.1", 21, "administrator", "zyuc2011", "test", "test.txt", input);
  23. System.out.println(flag);
  24. } catch (UnsupportedEncodingException e) {
  25. e.printStackTrace();
  26. }
  27. }
复制代码

二、文件下载
文件下载源代码

  1. /**
  2. * Description: 从FTP服务器下载文件
  3. * @Version1.0
  4. * @param url FTP服务器hostname
  5. * @param port FTP服务器端口
  6. * @param username FTP登录账号
  7. * @param password FTP登录密码
  8. * @param remotePath FTP服务器上的相对路径
  9. * @param fileName 要下载的文件名
  10. * @param localPath 下载后保存到本地的路径
  11. * @return
  12. */
  13. public static boolean downFile(
  14. String url, //FTP服务器hostname
  15. int port,//FTP服务器端口
  16. String username, //FTP登录账号
  17. String password, //FTP登录密码
  18. String remotePath,//FTP服务器上的相对路径
  19. String fileName,//要下载的文件名
  20. String localPath//下载后保存到本地的路径
  21. ) {
  22. boolean success = false;
  23. FTPClient ftp = new FTPClient();
  24. try {
  25. int reply;
  26. ftp.connect(url, port);
  27. //如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
  28. ftp.login(username, password);//登录
  29. reply = ftp.getReplyCode();
  30. if (!FTPReply.isPositiveCompletion(reply)) {
  31. ftp.disconnect();
  32. return success;
  33. }
  34. ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录
  35. FTPFile[] fs = ftp.listFiles();
  36. for(FTPFile ff:fs){
  37. if(ff.getName().equals(fileName)){
  38. File localFile = new File(localPath+"/"+ff.getName());
  39. OutputStream is = new FileOutputStream(localFile);
  40. ftp.retrieveFile(ff.getName(), is);
  41. is.close();
  42. }
  43. }
  44. ftp.logout();
  45. success = true;
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. } finally {
  49. if (ftp.isConnected()) {
  50. try {
  51. ftp.disconnect();
  52. } catch (IOException ioe) {
  53. }
  54. }
  55. }
  56. return success;
  57. }
复制代码

以下是文件下载的测试用例:

  1. /**
  2. * 将FTP服务器上文件下载到本地
  3. *
  4. */
  5. public void testDownFile(){
  6. try {
  7. boolean flag = downFile("127.0.0.1", 21, "administrator", "zyuc2011", "test", "test.txt", "D:/");
  8. System.out.println(flag);
  9. } catch (Exception e) {
  10. e.printStackTrace();
  11. }
  12. }
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助。

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-8 18:58

Copyright 2015-2025 djqfx

返回顶部