0

我正在编写一个必须连接到 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)

任何帮助,将不胜感激。

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

4

1 回答 1

2

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

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();
    }
于 2015-11-30T18:02:42.007 回答