一:本文采用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
如图

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.
上传:
public class MyFtp {
public static void main(String[] args) {
try {
FTPClient ftp = new FTPClient();
ftp.connect("127.0.0.1", 21);
boolean isLogin = ftp.login("a", "a");
System.out.println("登录:"+isLogin);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
boolean isStore = ftp.storeFile("note.txt", new FileInputStream("d:/note.txt"));
ftp.storeFile("1.png", new FileInputStream("d:/1.png"));
System.out.println("上传:"+isStore);
} catch (Exception e) {
e.printStackTrace();
}
}
}
下载:
public class MyFtp {
public static void main(String[] args) {
try {
FTPClient ftp = new FTPClient();
ftp.connect("127.0.0.1", 21);
boolean isLogin = ftp.login("a", "a");
System.out.println("登录:"+isLogin);
// ftp.setFileType(FTP.BINARY_FILE_TYPE);
//
// boolean isStore = ftp.storeFile("note.txt", new FileInputStream("d:/note.txt"));
//
// ftp.storeFile("1.png", new FileInputStream("d:/1.png"));
//
// System.out.println("上传:"+isStore);
boolean isDown = ftp.retrieveFile("note.txt", new FileOutputStream("d:/TDDOWNLOAD/note.txt"));
isDown = ftp.retrieveFile("1.png", new FileOutputStream("d:/TDDOWNLOAD/note.png"));
System.out.println("下载:"+isDown);
} catch (Exception e) {
e.printStackTrace();
}
}
}
本文介绍了如何利用Apache Commons Net库搭建FTP服务器,并通过FileZilla进行安装和配置。同时提供了Java代码示例,展示了如何使用FTPClient进行文件上传和下载操作。
1432

被折叠的 条评论
为什么被折叠?



