我尝试使用org.apache.commons.net.ftp.FTPClient通过安全FTP上传文件,但我收到" java.net.NoRouteToHostException:没有到主机的路由"连接时主机是可ping的,我甚至可以通过Filezilla SFTP连接使用我的FTP用户读/写文件。我的路由器没有激活防火墙。 可能是什么问题?
我的规格:薄荷17 x64,Java 7,路由器Netgear DGND4000(也许这个帮助)
这是代码:
package it.eproject.pdf;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTPClient;
public class Test {
public static void main(String[] args) {
System.out.println("Start upload file");
String sourceFile = "pdf/d00001.pdf";
String pdfName = "d00001.pdf";
String pdfDestFolder = "/pdf";
String ftpHost = "itsa.pingable.host";
String ftpUsername = "username";
String ftpPassword = "password";
FTPClient ftp = new FTPClient();
try {
ftp.setConnectTimeout(5000);
ftp.connect(ftpHost);
ftp.login(ftpUsername, ftpPassword);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
InputStream input = new FileInputStream(new File(pdfName));
ftp.storeFile(pdfDestFolder + pdfName, input);
ftp.logout();
ftp.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Done");
}
}
这是例外:
java.net.NoRouteToHostException: No route to host
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:182)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:203)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:296)
at it.eproject.pdf.Test.main(Test.java:26)
答案 0 :(得分:0)
SFTP(安全FTP)与FTPS(基于SSL的FTP)不同。确保你没有混合苹果和橘子。
答案 1 :(得分:0)
我认为Apache Commons libs不提供SFTP API,所以我切换到JCraft Jsch(http://www.jcraft.com/jsch/),它就像一个魅力。