-【Java FTP及FTP服务器搭建】

来源:互联网 发布:灰色西装配马甲 知乎 编辑:程序博客网 时间:2024/06/05 15:20

-【Java FTP及FTP服务器搭建】


一:本文采用apache项目组的

Apache Commons Net™ library


项目地址:http://commons.apache.org/net/


如下图:可见FTP只是其中一个支持的协议,还有很多其他,如有需要的同学,可参考官方网站。

Features

Supported protocols include:

  • FTP/FTPS
  • FTP over HTTP (experimental)
  • NNTP
  • SMTP(S)
  • POP3(S)
  • IMAP(S)
  • Telnet
  • TFTP
  • Finger
  • Whois
  • rexec/rcmd/rlogin
  • Time (rdate) and Daytime
  • Echo
  • Discard
  • NTP/SNTP

二:搭建ftp服务器


1:下载filezilla

http://filezilla-project.org/


如图


2:安装到windows


双击,下一步,完成!


3:启动ftp服务器

双击桌面图标,输入PC的密码

登录成功


4:ftp添加 一个用户,并设置共享文件夹




5:测试


简单的ftp server完成。


三:java代码


FTPClientFTPClient encapsulates all the functionality necessary to store and retrieve files from an FTP server.


上传:

[java] view plaincopyprint?
  1. public class MyFtp {  
  2.   
  3.     public static void main(String[] args) {  
  4.           
  5.         try {  
  6.               
  7.             FTPClient ftp = new FTPClient();  
  8.               
  9.             ftp.connect("127.0.0.1"21);  
  10.               
  11.             boolean isLogin = ftp.login("a""a");  
  12.               
  13.             System.out.println("登录:"+isLogin);  
  14.               
  15.             ftp.setFileType(FTP.BINARY_FILE_TYPE);  
  16.               
  17.             boolean isStore = ftp.storeFile("note.txt"new FileInputStream("d:/note.txt"));  
  18.               
  19.             ftp.storeFile("1.png"new FileInputStream("d:/1.png"));  
  20.               
  21.             System.out.println("上传:"+isStore);  
  22.               
  23.         } catch (Exception e) {  
  24.             e.printStackTrace();  
  25.         }  
  26.           
  27.           
  28.     }  
  29. }  

下载:


[java] view plaincopyprint?
  1. public class MyFtp {  
  2.   
  3.     public static void main(String[] args) {  
  4.           
  5.         try {  
  6.               
  7.             FTPClient ftp = new FTPClient();  
  8.               
  9.             ftp.connect("127.0.0.1"21);  
  10.               
  11.             boolean isLogin = ftp.login("a""a");  
  12.               
  13.             System.out.println("登录:"+isLogin);  
  14.               
  15. //          ftp.setFileType(FTP.BINARY_FILE_TYPE);  
  16. //             
  17. //          boolean isStore = ftp.storeFile("note.txt", new FileInputStream("d:/note.txt"));  
  18. //             
  19. //          ftp.storeFile("1.png", new FileInputStream("d:/1.png"));  
  20. //             
  21. //          System.out.println("上传:"+isStore);  
  22.               
  23.             boolean isDown = ftp.retrieveFile("note.txt"new FileOutputStream("d:/TDDOWNLOAD/note.txt"));  
  24.             isDown = ftp.retrieveFile("1.png"new FileOutputStream("d:/TDDOWNLOAD/note.png"));  
  25.             System.out.println("下载:"+isDown);  
  26.         } catch (Exception e) {  
  27.             e.printStackTrace();  
  28.         }  
  29.           
  30.           
  31.     }  
  32. }  
原创粉丝点击