我正在编写一个程序,该程序必须连接到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)
任何帮助,将不胜感激。
另外,我没有服务器的登录名,它只是文件的公共存储库。这会影响我的工作方式吗?
最佳答案
您需要分割路径并创建一个类似于以下内容的网址:
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();
}