FTPClient
我正在准备使用 Java ( )从远程 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){}
我需要将文件保存到名为 test.txt 的 contextFolder 中 :) 谢谢大家 :)
这是我得到的例外
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)