簡體   English   中英

從遠程FTP服務器下載文件(文件下載為空)

[英]Download File from remote Ftp Server (file downloaded empty)

我正在准備使用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){}

我需要將文件保存到名為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)

您是否收到“文件XXX已下載”消息? 如果不是,則可能的原因是引發異常,並由此將其靜默丟棄:

} catch (Exception ex){}

您永遠都不應捕獲此類異常。

關閉它之前,請嘗試使用outputStream.flush()

如果那沒有幫助,您應該檢查try塊中是否通過ex.printStackTrace()拋出了任何異常。 您經常在那里找到原因(例如,誤寫權限,連接錯誤等)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM