我正在尝试使用Java (FTPClient)从远程ftp服务器下载一个文件。该文件已成功下载,但它是空的,我没有找到解决我的问题的办法
这是我的密码
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(ip, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.epsv();
ftpClient.mlsd();
File downloadFile = new File("contextFolder/test.txt");
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));
InputStream inputStream = ftpClient.retrieveFileStream(remoteFile);
byte[] bytesArray = new byte[4096];
int length;
//copy the file content in bytes
while ((length = inputStream.read(bytesArray)) > 0){
outputStream.write(bytesArray, 0, length);
}
Boolean success = ftpClient.completePendingCommand();
outputStream.close();
inputStream.close();
if (success) {
System.out.println("File "+remoteFile+" has been downloaded successfully.");
}
}catch(Exception ex){}我需要将文件保存到名为contextFolder的test.txt :)中,谢谢大家:)
这是我得到的例外
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.read(Unknown Source)
at org.apache.commons.net.io.CRLFLineReader.readLine(CRLFLineReader.java:58)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:314)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:294)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:483)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:556)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:605)
at org.apache.commons.net.ftp.FTP.pasv(FTP.java:956)
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:806)
at org.apache.commons.net.ftp.FTPClient._retrieveFileStream(FTPClient.java:1853)
at org.apache.commons.net.ftp.FTPClient.retrieveFileStream(FTPClient.java:1844)
at com.ericsson.etl.module.Activity1.execute(Activity1.java:49)
at com.ericsson.etl.SequenceProcessor.doActivities(SequenceProcessor.java:130)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)发布于 2013-05-08 11:11:19
您是否收到“文件XXX已被下载”的消息?如果不是,则可能的原因是抛出异常,并由以下内容默默丢弃:
} catch (Exception ex){}你永远不应该抓住这样的例外。
发布于 2013-05-08 11:16:32
在关闭outputStream.flush()之前尝试它。
如果这没有帮助,您应该检查在try块中是否有任何异常是通过ex.printStackTrace()引发的。您经常会发现其中的原因(例如,误写权限、连接错误、.)。
https://stackoverflow.com/questions/16439054
复制相似问题