2

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)
4

2 回答 2

1

您是否收到“文件 XXX 已下载”消息?如果不是,则可能的原因是抛出异常,并被此静默丢弃:

} catch (Exception ex){}

你永远不应该捕获这样的异常。

于 2013-05-08T11:11:19.320 回答
0

outputStream.flush()在关闭它之前尝试。

如果这没有帮助,您应该检查是否ex.printStackTrace()在 try 块中引发了任何异常。您经常会在那里找到原因(例如,缺少写入权限、连接错误……)。

于 2013-05-08T11:16:32.820 回答