Java实现FTP上传下载文件的工具包有很多,这里我采用Java自带的API,实现FTP上传下载文件。另外JDK1.7以前的版本与其之后版本的API有了较大的改变了。
例如:
| JDK1.7之前 | JDK1.7 |
| ftpClient = new FtpClinet() | ftpClient = FtpClient.create(ip) |
| ftpclient.login(user,password) | ftpclient.login(user,null,password) |
| ftpclient.binary() | ftpClient.setBinaryType(); |
一. 连接FTP服务器
class FTPUtil {
//FTP服务器IP地址
;
4:
//FTP服务器端口
int FTP_PORT = 21;
7:
//FTP服务器用户名
;
10:
//密码
;
13:
14:
static FtpClient getConnect()
16: {
try {
18: FtpClient ftpClient = FtpClient.create(FTP_HOST);
19: ftpClient.login(FTP_USER, FTP_PASSWORD.toCharArray());
return ftpClient;
catch (FtpProtocolException e) {
// TODO Auto-generated catch block
23: e.printStackTrace();
);
return null;
catch (IOException e) {
// TODO Auto-generated catch block
28: e.printStackTrace();
);
return null;
31: }
32:
33: }
34: }