LOFTER for ipad —— 让兴趣,更有趣

点击下载 关闭
推荐一个java操作ftp的工具类
charmsongo 2018-12-08

写在前面

作为经常使用电脑整理文件的童鞋,应该都使用过从ftp服务器上传下载文件,那么今天就了解下如何通过java程序操作ftp服务的文件

首先你要知道ftp的ip,路径,端口,有操作权限的账号和密码

1 导入jar包

  1. commons-net-3.6.jar

这个jar包用来设置编码,经过测试,不加也可用

2 工具类中主要方法

2.1 登陆ftp

  1. /**

  2.      * 验证登录

  3.      * @param ip

  4.      * @param port

  5.      * @param name

  6.      * @param pwd

  7.      * @return

  8.      */

  9. public boolean login(String ip,int port,String name,String pwd){

  10. try{

  11.             ftp =newFTPClient();

  12.             ftp.connect(ip, port);

  13. System.out.println(ftp.login(name, pwd));

  14. if(!ftp.login(name, pwd)){

  15. returnfalse;

  16. }

  17.             ftp.setCharset(Charset.forName("UTF-8"));

  18.             ftp.setControlEncoding("UTF-8");

  19. }catch(IOException e){

  20.             e.printStackTrace();

  21. returnfalse;

  22. }

  23. returntrue;

  24. }

注意:获取远程文件目录,上传和下载方法基于登陆方法

2.2 获取远程文件目录
  1. /**

  2.      * 获取ftp某一文件(路径)下的文件名字,用于查看文件列表

  3.      * @param ip

  4.      * @param port

  5.      * @param name

  6.      * @param pwd

  7.      * @param remotedir 远程地址目录

  8.      * @return

  9.      */

  10. publicboolean getFilesName(String ip,int port,String name,String pwd,String remotedir){

  11. try{

  12. if(!login(ip, port, name, pwd)){

  13. returnfalse;

  14. }

  15. //获取ftp里面,指定文件夹 里面的文件名字,存入数组中

  16. FTPFile[] files = ftp.listFiles(remotedir);

  17. //打印出ftp里面,指定文件夹 里面的文件名字

  18. for(int i =0; i < files.length; i++){

  19. System.out.println(files[i].getName());

  20. }

  21. }catch(IOException e){

  22.             e.printStackTrace();

  23. returnfalse;

  24. }finally{

  25. this.close();

  26. }

  27. returntrue;

  28. }

2.3 上传文件

  1. /**

  2.      * 上传文件 方法一

  3.      * @param ip

  4.      * @param port

  5.      * @param name

  6.      * @param pwd

  7.      * @param remotepath 远程地址文件路径

  8.      * @param localpath 本地文件路径

  9.      * @return

  10.      */

  11. publicboolean putFileOne(String ip,int port,String name,String pwd,String remotepath,String localpath){

  12. try{

  13. if(!login(ip, port, name, pwd)){

  14. returnfalse;

  15. }

  16. //将本地的 localpath 文件上传到ftp的根目录文件夹下面,并重命名为 remotepath中的名字

  17. return ftp.storeFile(remotepath,newFileInputStream(newFile(localpath)));

  18. }catch(IOException e){

  19.             e.printStackTrace();

  20. returnfalse;

  21. }finally{

  22. this.close();

  23. }

  24. }

  25. /**

  26.      * 上传文件的第二种方法,优化了传输速度

  27.      * @param ip

  28.      * @param port

  29.      * @param name

  30.      * @param pwd

  31.      * @param remotepath 远程地址文件路径

  32.      * @param localpath 本地文件路径

  33.      * @return

  34.      */

  35. publicboolean putFileTwo(String ip,int port,String name,String pwd,String remotepath,String localpath){

  36. try{

  37. if(!login(ip, port, name, pwd)){

  38. returnfalse;

  39. }

  40.             os = ftp.storeFileStream(remotepath);

  41.             fis =newFileInputStream(newFile(localpath));

  42. byte[] b =newbyte[1024];

  43. int len =0;

  44. while((len = fis.read(b))!=-1){

  45.                 os.write(b,0,len);

  46. }

  47. }catch(IOException e){

  48.             e.printStackTrace();

  49. returnfalse;

  50. }finally{

  51. this.close();

  52. }

  53. returntrue;

  54. }

2.4 下载文件

  1. /**

  2.      * 下载文件 方法一

  3.      * @param ip

  4.      * @param port

  5.      * @param name

  6.      * @param pwd

  7.      * @param remotepath 远程地址文件路径

  8.      * @param localpath 本地文件路径

  9.      * @return

  10.      */

  11. publicboolean getFileOne(String ip,int port,String name,String pwd,String remotepath,String localpath){

  12. try{

  13. if(!login(ip, port, name, pwd)){

  14. returnfalse;

  15. }

  16. //将ftp资源中 remotepath 文件下载到本地目录文件夹下面,并重命名为 localpath 中的名字

  17. return ftp.retrieveFile(remotepath,newFileOutputStream(newFile(localpath)));

  18. }catch(IOException e){

  19.             e.printStackTrace();

  20. returnfalse;

  21. }finally{

  22. this.close();

  23. }

  24. }

  25. /**

  26.      * 下载文件的第二种方法,优化了传输速度

  27.      * @param ip

  28.      * @param port

  29.      * @param name

  30.      * @param pwd

  31.      * @param remotepath 远程地址文件路径

  32.      * @param localpath  本地文件路径

  33.      * @return

  34.      */

  35. publicboolean getFileTwo(String ip,int port,String name,String pwd,String remotepath,String localpath){

  36. try{

  37. if(!login(ip, port, name, pwd)){

  38. returnfalse;

  39. }

  40.             is = ftp.retrieveFileStream(remotepath);

  41.             fos =newFileOutputStream(newFile(localpath));

  42. byte[] b =newbyte[1024];

  43. int len =0;

  44. while((len = is.read(b))!=-1){

  45.                 fos.write(b,0,len);

  46. }

  47. }catch(IOException e){

  48.             e.printStackTrace();

  49. returnfalse;

  50. }finally{

  51. this.close();

  52. }

  53. returntrue;

  54. }

3 源码

当然上面代码只是重要的部分,如果有问题可去github自行下载 charmsongo

如果有什么更好的方法欢迎留言


推荐文章
评论(0)
分享到
转载我的主页