简体   繁体   中英

Unable to connect to FTP using FTP4J

I'm writing a program that has to connect to an FTP server in order to download certain files. In order to do this I'm using the FTP4J library, However I'm running into some trouble.

So far I have:

    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();
        }

Where the URL of the FTP is ftp://ftp.pride.ebi.ac.uk/pride/data/archive/2015/10/PXD002829

However If I run the program I get:

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)

Any help would be appreciated.

Also I don't have a log in for the server, it's just a public repository of files. Will this effect how I go about doing things?

You need to split off the path and create a url that looks like:

ftp.pride.ebi.ac.uk

In answer to your comment you need to do something like this:

    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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM