ftp url java_无法在java中连接FTP服务器

我试图用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.(Unknown Source)

at java.net.Socket.(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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值