JAVA中使用FTPClient上传下载

本文介绍如何使用Jakarta Commons中的FTPClient进行文件上传和下载操作。包括上传本地文件到FTP服务器及在服务器上生成文件并写入内容的方法,同时提供从FTP服务器下载文件到本地的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JAVA中使用FTPClient上传下载

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

一、上传文件

原理就不介绍了,大家直接看代码吧

[Java] view plain copy
  1. /**
  2. *Description:向FTP服务器上传文件
  3. *@Version1.0Jul27,20084:31:09PMby崔红保(cuihongbao@d-heaven.com)创建
  4. *@paramurlFTP服务器hostname
  5. *@paramportFTP服务器端口
  6. *@paramusernameFTP登录账号
  7. *@parampasswordFTP登录密码
  8. *@parampathFTP服务器保存目录
  9. *@paramfilename上传到FTP服务器上的文件名
  10. *@paraminput输入流
  11. *@return成功返回true,否则返回false
  12. */
  13. publicstaticbooleanuploadFile(Stringurl,intport,Stringusername,Stringpassword,Stringpath,Stringfilename,InputStreaminput){
  14. booleansuccess=false;
  15. FTPClientftp=newFTPClient();
  16. try{
  17. intreply;
  18. ftp.connect(url,port);//连接FTP服务器
  19. //如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
  20. ftp.login(username,password);//登录
  21. reply=ftp.getReplyCode();
  22. if(!FTPReply.isPositiveCompletion(reply)){
  23. ftp.disconnect();
  24. returnsuccess;
  25. }
  26. ftp.changeWorkingDirectory(path);
  27. ftp.storeFile(filename,input);
  28. input.close();
  29. ftp.logout();
  30. success=true;
  31. }catch(IOExceptione){
  32. e.printStackTrace();
  33. }finally{
  34. if(ftp.isConnected()){
  35. try{
  36. ftp.disconnect();
  37. }catch(IOExceptionioe){
  38. }
  39. }
  40. }
  41. returnsuccess;
  42. }<PRE>

下面我们写两个小例子:

1.将本地文件上传到FTP服务器上,代码如下:

[Java] view plain copy
  1. @Test
  2. publicvoidtestUpLoadFromDisk(){
  3. try{
  4. FileInputStreamin=newFileInputStream(newFile("D:/test.txt"));
  5. booleanflag=uploadFile("127.0.0.1",21,"test","test","D:/ftp","test.txt",in);
  6. System.out.println(flag);
  7. }catch(FileNotFoundExceptione){
  8. e.printStackTrace();
  9. }
  10. }<PRE>

2.在FTP服务器上生成一个文件,并将一个字符串写入到该文件中

[Java] view plain copy
  1. @Test
  2. publicvoidtestUpLoadFromString(){
  3. try{
  4. InputStreaminput=newByteArrayInputStream("testftp".getBytes("utf-8"));
  5. booleanflag=uploadFile("127.0.0.1",21,"test","test","D:/ftp","test.txt",input);
  6. System.out.println(flag);
  7. }catch(UnsupportedEncodingExceptione){
  8. e.printStackTrace();
  9. }
  10. }<PRE>


二、下载文件

从FTP服务器下载文件的代码也很简单,参考如下:

[Java] view plain copy
  1. /**
  2. *Description:从FTP服务器下载文件
  3. *@Version1.0Jul27,20085:32:36PMby崔红保(cuihongbao@d-heaven.com)创建
  4. *@paramurlFTP服务器hostname
  5. *@paramportFTP服务器端口
  6. *@paramusernameFTP登录账号
  7. *@parampasswordFTP登录密码
  8. *@paramremotePathFTP服务器上的相对路径
  9. *@paramfileName要下载的文件名
  10. *@paramlocalPath下载后保存到本地的路径
  11. *@return
  12. */
  13. publicstaticbooleandownFile(Stringurl,intport,Stringusername,Stringpassword,StringremotePath,StringfileName,StringlocalPath){
  14. booleansuccess=false;
  15. FTPClientftp=newFTPClient();
  16. try{
  17. intreply;
  18. ftp.connect(url,port);
  19. //如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
  20. ftp.login(username,password);//登录
  21. reply=ftp.getReplyCode();
  22. if(!FTPReply.isPositiveCompletion(reply)){
  23. ftp.disconnect();
  24. returnsuccess;
  25. }
  26. ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录
  27. FTPFile[]fs=ftp.listFiles();
  28. for(FTPFileff:fs){
  29. if(ff.getName().equals(fileName)){
  30. FilelocalFile=newFile(localPath+"/"+ff.getName());
  31. OutputStreamis=newFileOutputStream(localFile);
  32. ftp.retrieveFile(ff.getName(),is);
  33. is.close();
  34. }
  35. }
  36. ftp.logout();
  37. success=true;
  38. }catch(IOExceptione){
  39. e.printStackTrace();
  40. }finally{
  41. if(ftp.isConnected()){
  42. try{
  43. ftp.disconnect();
  44. }catch(IOExceptionioe){
  45. }
  46. }
  47. }
  48. returnsuccess;
  49. }<PRE>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值