这个问题困扰了我一个星期,
我正在尝试使用 java 上传带有 ftp 的文件 xml,没什么太花哨的。
经过一些网络搜索后,我决定使用 ftp4j 包,根据文档应该可以解决问题。
关键是这个 ftp 服务器实际上与 Filezilla 一起工作......
这些是我的代码的几行:
public void connect() {
try {
String[] con = client.connect(s);
for (int i = 0; i < con.length; i++) {
System.out.println(con[i]);
}
client.login(user, password);
s = client.getHost();
System.out.println(s);
con = client.help();
for (int i = 0; i < con.length; i++) {
System.out.println(con[i]);
}
client.changeDirectory(sDir);
s = client.currentDirectory();
System.setProperty("ftp4j.passiveDataTransfer.useSuggestedAddress", "1");
client.setPassive(true);
System.out.println(client.isPassive());
client.setType(FTPClient.TYPE_BINARY);
client.upload(file);
} catch (Exception ex) {
Logger.getLogger(ftpConnector.class.getName()).log(Level.SEVERE, null, ex);
System.out.println(ex.toString());
}
}
好吧,我得到了这个输出:
*
*
* Aruba.it
# The Web Hosting Company
#
#
This is a private system - No anonymous login
ftp.open-eye.it
The following SITE commands are recognized
CHMOD
IDLE
UTIME
Pure-FTPd - http://pureftpd.org/
true
[code=200, message=TYPE is now ASCII]
java.net.SocketException: Connection reset
我很确定连接重置与我使用 client.upload(file); 提供的上传命令有关。
我所有的变量都在上层声明。
ava.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:189)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at sun.nio.cs.StreamDecoder.read0(StreamDecoder.java:126)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:112)
at java.io.InputStreamReader.read(InputStreamReader.java:168)
at it.sauronsoftware.ftp4j.NVTASCIIReader.readLine(NVTASCIIReader.java:105)
at it.sauronsoftware.ftp4j.FTPCommunicationChannel.read(FTPCommunicationChannel.java:142)
at it.sauronsoftware.ftp4j.FTPCommunicationChannel.readFTPReply(FTPCommunicationChannel.java:187)
at it.sauronsoftware.ftp4j.FTPClient.openPassiveDataTransferChannel(FTPClient.java:3534)
at it.sauronsoftware.ftp4j.FTPClient.openDataTransferChannel(FTPClient.java:3468)
at it.sauronsoftware.ftp4j.FTPClient.download(FTPClient.java:3297)
at it.sauronsoftware.ftp4j.FTPClient.download(FTPClient.java:3208)
at it.sauronsoftware.ftp4j.FTPClient.download(FTPClient.java:3073)
谢谢你的时间,弗朗西斯科。