我正在尝试使用简单的java程序使用FTP / S连接到我的FTP服务器。我使用的是JDK7u79和Apache Commons Net 3.3。
我在这里看过类似的问题: https://serverfault.com/questions/85027/how-do-i-setup-an-ftp-server-on-windows-7
FTPClient Uploading File = SocketException: Connection reset
这是我的connect()方法:
/**
* Connects and login to the FTP server
*/
public void connect() throws IOException {
int reply;
FTPSClient ftpsclient = null;
if(getFlavor().equalsIgnoreCase(FLAVOR_FTP)) {
client = new FTPClient();
} else if(getFlavor().equalsIgnoreCase(FLAVOR_FTPS)) {
client = new FTPSClient();
//client = ftpsclient;
((FTPSClient) client).setTrustManager(TrustManagerUtils.getAcceptAllTrustManager());
}
try {
client.connect(getHostname(), this.getPortInt());
} catch (IOException ioe) {
ioe.printStackTrace();
String errMsg = "Could not connect to the FTP site. Reason: " + ioe.getMessage();
throw new IOException(errMsg);
}
if(getFlavor().equalsIgnoreCase(FLAVOR_FTPS)) {
((FTPSClient) client).execPBSZ(0); // Set protection buffer size
((FTPSClient) client).execPROT("P"); // Set data channel protection to private
}
if (ACTIVE_MODE.equalsIgnoreCase(getConnectionMode())){
client.enterLocalActiveMode();
}else if (PASSIVE_MODE.equalsIgnoreCase(getConnectionMode())){
client.enterLocalPassiveMode();
}
// After connection attempt, you should check the reply code to verify success.
reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
error("connect", "FTP server " + getHostname() + " refused connection.");
throw new IOException("Failed to connect to FTP Server '" + getHostname() + "', Reply code '" + client.getReplyCode()+"'");
}
// login
client.login(this.getLoginname(), this.getPassword());
reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)){
throw new IOException("Failed to login to FTP Server '" + getHostname() + "' using '"
+ getLoginname() + "', Reply code '" + client.getReplyCode() + "'");
}
// mode change should be done only at connect()
this.changeMode(this.getFileMode());
// the same for server directory
this.changeServerDirectory(this.getServerDirectory());
reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)){
disconnect();
throw new IOException("Failed to change directory to [" + getServerDirectory() + "] on FTP Server [" + getHostname() + "]; Reply code=[" + client.getReplyCode() + "].");
}
message("Connect", "Successfully connected to FTP Server [" + getHostname() + "] using file mode=[" + getFileMode()+"]; Connection mode=[" + getConnectionModeString()+ "].");
}
我总是低于例外:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.read(Unknown Source)
at org.apache.commons.net.io.CRLFLineReader.readLine(CRLFLineReader.java:58)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:314)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:294)
at org.apache.commons.net.ftp.FTP._connectAction_(FTP.java:400)
at org.apache.commons.net.ftp.FTPClient._connectAction_(FTPClient.java:924)
at org.apache.commons.net.ftp.FTPSClient._connectAction_(FTPSClient.java:207)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:183)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:203)
at com.mycompany.FTPAdapter.connect(FTPAdapter.java:250)
我已尝试执行以下命令作为管理员,但没有运气:
netsh advfirewall set global StatefulFTP disable
我在Windows 7上使用IIS创建了自签名证书。我可以通过FileZilla使用FTP / S连接到该站点。它询问我提示信任证书,然后它可以连接到服务器。
有人可以指导我做错了吗?
答案 0 :(得分:0)
啊,我必须使用参数true:FTPSClient(true)创建FTPSClient,因为我在默认端口990上运行FTP / S.