gpt4 book ai didi

java - 无法使用 FTP4J 连接到 FTP

转载 作者:行者123 更新时间:2023-12-02 04:08:44 30 4
gpt4 key购买 nike

我正在编写一个程序,它必须连接到 FTP 服务器才能下载某些文件。为了做到这一点,我使用了 FTP4J 库,但是我遇到了一些麻烦。

到目前为止我已经:

    if ("Dataset FTP location".equals(link.text())) {

String FTPURL = link.attr("href");

FTPClient client = new FTPClient();

try {
client.connect(FTPURL);
} catch (FTPIllegalReplyException e) {
e.printStackTrace();
} catch (FTPException e) {
e.printStackTrace();
}

FTP 的 URL 为 ftp://ftp.pride.ebi.ac.uk/pride/data/archive/2015/10/PXD002829

但是,如果我运行该程序,我会得到:

Exception in thread "main" java.net.UnknownHostException: ftp://ftp.pride.ebi.ac.uk/pride/data/archive/2015/10/PXD002829
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at it.sauronsoftware.ftp4j.FTPConnector.tcpConnectForCommunicationChannel(FTPConnector.java:208)
at it.sauronsoftware.ftp4j.connectors.DirectConnector.connectForCommunicationChannel(DirectConnector.java:39)
at it.sauronsoftware.ftp4j.FTPClient.connect(FTPClient.java:1036)
at it.sauronsoftware.ftp4j.FTPClient.connect(FTPClient.java:1003)
at Main.main(Main.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

如有任何帮助,我们将不胜感激。

此外,我没有登录服务器,它只是一个公共(public)文件存储库。这会影响我做事的方式吗?

最佳答案

您需要拆分路径并创建一个如下所示的网址:

ftp.pride.ebi.ac.uk

为了回答您的评论,您需要执行以下操作:

    String ftpPath = "ftp://ftp.pride.ebi.ac.uk/pride/data/archive/2015/10/PXD002829";
URL url = new URL(ftpPath);
String host = url.getHost();
FTPClient client = new FTPClient();
try {
client.connect(host);
client.login("anonymous", "anonymous");
FTPFile[] list = client.list(url.getPath());
for (FTPFile f : list) {
// Instead of printing out the file download it. See
// http://www.sauronsoftware.it/projects/ftp4j/manual.php#14
System.out.println(f);
}
} catch (FTPIllegalReplyException e) {
e.printStackTrace();
} catch (FTPException e) {
e.printStackTrace();
}

关于java - 无法使用 FTP4J 连接到 FTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34004822/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com