java ftp上传不了文件怎么办
发布网友
发布时间:2022-07-12 08:38
我来回答
共1个回答
热心网友
时间:2023-10-17 04:35
准备条件:java实现ftp上传用到了commons-net-3.3.jar包
首先建立ftphost连接
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public boolean connect(String path, String addr, int port, String username, String password) {
try {
//FTPClient ftp = new FTPHTTPClient(addr, port, username, password);
ftp = new FTPClient();
int reply;
ftp.connect(addr);
System.out.println("连接到:" + addr + ":" + port);
System.out.print(ftp.getReplyString());
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.err.println("FTP目标服务器积极拒绝.");
System.exit(1);
return false;
}else{
ftp.login(username, password);
ftp.enterLocalPassiveMode();
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.changeWorkingDirectory(path);
System.out.println("已连接:" + addr + ":" + port);
return true;
}
} catch (Exception ex) {
ex.printStackTrace();
System.out.println(ex.getMessage());
return false;
}