我正在尝试使用Apache Commons Net在Java中开发FTP客户端,但即使我的代码看起来很好,我也无法连接到任何服务器而且我得到:
java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at org.apache.commons.net.ftp.FTPHTTPClient.connect(FTPHTTPClient.java:131)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:296)
at awax.meteosat.test.FTPClientTest.connect(FTPClientTest.java:52)
at awax.meteosat.test.FTPClientTest.main(FTPClientTest.java:146)
client disconnected
Exception in thread "main" java.lang.IllegalStateException: Client disconnected
at awax.meteosat.test.FTPClientTest.login(FTPClientTest.java:100)
at awax.meteosat.test.FTPClientTest.main(FTPClientTest.java:147)
我尝试连接到此服务器:ftp://ics.ftptest.org/,效果很好。我试图断开我的防火墙,并且利用相同的结果。
这是我的代码,我希望对你们来说很明显哈哈:
package awax.meteosat.test;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.URL;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPHTTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FTPClientTest {
private final String host;
private final int port;
private final String username;
private final String password;
private org.apache.commons.net.ftp.FTPClient client;
/**
* Instanciate FTP server.
*
* @param host
* FTP server address.
* @param username
* FTP username.
* @param password
* FTP password.
*/
public FTPClientTest (final String address, final String username, final String password) {
URL url = null;
try {
url = new URL(address);
} catch (MalformedURLException e) {
e.printStackTrace();
}
this.host = url != null ? url.getHost() : null;
this.port = 0;
this.username = username;
this.password = password;
}
public void connect () {
if (this.client == null) {
this.client = new FTPHTTPClient(this.host, this.port, this.username, this.password);
try {
// Connecting client
if (this.port > 0) {
this.client.connect(this.host, this.port);
} else {
this.client.connect(this.host);
}
// Check connection
int reply = this.client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
disconnect();
} else {
setFtpOptions();
}
} catch (SocketException e) {
e.printStackTrace();
disconnect();
} catch (IOException e) {
e.printStackTrace();
disconnect();
}
} else {
System.err.println("client is null");
}
}
public void disconnect () {
if (this.client != null) {
if (this.client.isConnected()) {
try {
this.client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.err.println("client disconnected");
}
} else {
System.err.println("client is null");
}
}
public boolean login () {
if (isConnected()) {
try {
return this.client.login(this.username, this.password);
} catch (IOException e) {
e.printStackTrace();
logout();
return false;
}
} else {
throw new IllegalStateException("Client disconnected");
}
}
public boolean logout () {
if (isConnected()) {
try {
return this.client.logout();
} catch (IOException e) {
e.printStackTrace();
return false;
}
} else {
throw new IllegalStateException("Client disconnected");
}
}
public FTPFile[] listDirectories () {
if (isConnected()) {
try {
return this.client.listDirectories();
} catch (IOException e) {
e.printStackTrace();
return null;
}
} else {
throw new IllegalStateException("Client disconnected");
}
}
public boolean isConnected () {
if (this.client != null) {
return this.client.isConnected();
} else {
return false;
}
}
private void setFtpOptions () {
if (isConnected()) {
this.client.enterLocalPassiveMode();
}
}
public static void main (String args[]) {
FTPClientTest ftp = new FTPClientTest("ftp://ics.ftptest.org/", "", "");
ftp.connect();
ftp.login();
for (FTPFile f : ftp.listDirectories()) {
System.err.println(f);
}
ftp.logout();
ftp.disconnect();
}
}
答案 0 :(得分:2)
尝试使用FTPClient代替FTPHTTPClient(根据它的documentation是FTP连接的实验性HTTP代理)...
public void connect () {
if (this.client == null) {
this.client = new FTPClient();
应该可以正常工作