我正在尝试为FTPClient
个文件实施upload/download
。
我没有收到任何错误,但文件未上传。
我将我的FTP服务器配置为指向F:\ drive
并允许777为ftp_user
。
当我想显示文件夹内容时,它也会超时。
我的代码:
public static void main(String[] args)
{
FTPFunctions ftpobj = new FTPFunctions("xxx.xxx.x.xxx", 21, "ftp_user", "Xxxxxxxxx");
ftpobj.uploadFTPFile("F:\\FILES\\upload\\FTP_Test", "FTP_Test", "/FILES/FileStorage/");
ftpobj.listFTPFiles("/FILES/FileStorage/", "");
ftpobj.disconnect();
}
public FTPFunctions(String host, int port, String username, String password) throws Exception{
ftp = new FTPClient();
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
int reply;
ftp.connect(host, port);
System.out.println("FTP URL is:"+ftp.getDefaultPort());
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
throw new Exception("Exception in connecting to FTP Server");
}
ftp.login(username, password);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
}
public void uploadFTPFile(String localFileFullName, String fileName, String hostDir) throws Exception {
try {
InputStream input = new FileInputStream(new File(localFileFullName));
this.ftp.storeFile(hostDir + fileName, input);
}
catch(Exception e)
}
输出:
220 Microsoft FTP Service
FTP URL is:21
USER ftp_user
331 Password required for ftp_user.
PASS Xxxxxxxxx
230 User ftp_user logged in.
TYPE I
200 Type set to I.
PASV
227 Entering Passive Mode (xxx,xxx,x,xxx,208,106).
java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:924)
at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:657)
at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:643)
at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:2033)
at mil.dla.daps.ftp.FTPFunctions.uploadFTPFile(FTPFunctions.java:60)
at mil.dla.daps.ftp.FTPFunctions.main(FTPFunctions.java:121)
答案 0 :(得分:1)
uploadFTPFile方法中可能存在异常。尝试将方法更改为
public void uploadFTPFile(String localFileFullName, String fileName, String hostDir) throws Exception {
try {
InputStream input = new FileInputStream(new File(localFileFullName));
this.ftp.storeFile(hostDir + fileName, input);
}
catch(Exception e) {
e.printStrackTrace();
}
}
并查看是否打印出任何例外情况。
编辑1
也许ftp服务器不支持被动模式?尝试删除
ftp.enterLocalPassiveMode();
在登录后直接添加以下行
for (String file : ftp.listFiles("/")) {
System.out.println("File: " + file);
}