2015-11-30 46 views
0

我正在編寫一個程序,該程序必須連接到FTP服務器才能下載某些文件。爲了做到這一點,我使用FTP4J庫,但我遇到了一些麻煩。無法使用FTP4J連接到FTP

到目前爲止,我有:

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) 

任何幫助,將不勝感激。

另外我沒有登錄服務器,它只是一個公共的文件存儲庫。這會影響我如何去做事嗎?

回答

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

所以,你說我能只是直接進入指定的文件夾?說如果除了進入該頁面並從中下載文件,我不需要做任何事情,對我來說有沒有一種簡單的方法呢? – user2320239

+0

查看我的答案和更新的例子。你應該能夠弄清楚如何從文檔中下載文件。 – rainkinz

+0

@ user2320239這是正確答案嗎?如果是這樣,你可以這樣標記嗎?謝謝 – rainkinz