0

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?

rainkinz
  • 10,082
  • 5
  • 45
  • 73
user2320239
  • 1,021
  • 2
  • 18
  • 43

1 Answers1

2

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();
    }
rainkinz
  • 10,082
  • 5
  • 45
  • 73
  • So your saying I can't just go directly to the folder specified? Say if I don't need to do any thing apart from go to that page and download the files from it, is there an easy way for me to do that? – user2320239 Nov 30 '15 at 18:05
  • See my answer with an updated example. You should be able to figure out how to download the files from their documentation. – rainkinz Nov 30 '15 at 20:27
  • @user2320239 is this the correct answer? If so can you mark it as such? Thanks – rainkinz Dec 02 '15 at 14:22