簡體   English   中英

從 FTP 下載到帶有 Apache Common Net 的設備的損壞的 PDF 文件

[英]Corrupted PDF file downloaded from FTP to a device with Apache Common Net

我正在嘗試將文件從 FileZilla 服務器下載到 Android 設備。 一切似乎都正確,與 FTP 的連接、登錄、文件存在並且下載的響應正常。 但是當我轉到 Android 設備中的文件時,它只有 64KB 的大小,它必須是並且由於損壞而我無法打開......我嘗試了另一個 49KB 的文件並下載了 48KB,它也損壞了。 我正在使用 Adob​​e Acrobat Reader 和 QuickOffice 打開文件。

String usuarioLogin = params[0];

FTPClient ftp = null;
String LOG_TAG = "FTP";
ftp = new FTPClient();
String server = params[0];
int portNumber = Integer.parseInt(params[1]);
String user = params[2];
String password = params[3];
String filename = params[4];
File localFile = UtilsVictor.crearFichero(params[5]);

ftp.connect(server, portNumber);
Log.d(LOG_TAG, "Connected. Reply: " + ftp.getReplyString());
ftp.enterLocalPassiveMode();

ftp.login(user, password);
Log.d(LOG_TAG, "Logged in");
ftp.setFileType(FTP.LOCAL_FILE_TYPE);
Log.d(LOG_TAG, "Downloading");

OutputStream outputStream = null;
boolean success = false;
outputStream = new BufferedOutputStream(new FileOutputStream(
        localFile));
success = ftp.retrieveFile(filename, outputStream);

if(success) {
    Log.d(LOG_TAG, "success!!");
} else{
    Log.d(LOG_TAG, "NOOOOT success!!");
}

FileZilla 服務器的日志:

(000019)13/01/2018 17:13:14 - (not logged in) (192.168.1.38)> Connected on port 50000, sending welcome message...
(000019)13/01/2018 17:13:14 - (not logged in) (192.168.1.38)> 220-FileZilla Server 0.9.60 beta
(000019)13/01/2018 17:13:14 - (not logged in) (192.168.1.38)> 220-written by Tim Kosse (tim.kosse@filezilla-project.org)
(000019)13/01/2018 17:13:14 - (not logged in) (192.168.1.38)> 220 Please visit https://filezilla-project.org/
(000019)13/01/2018 17:13:14 - (not logged in) (192.168.1.38)> USER VICTOR
(000019)13/01/2018 17:13:14 - (not logged in) (192.168.1.38)> 331 Password required for victor
(000019)13/01/2018 17:13:14 - (not logged in) (192.168.1.38)> PASS ****
(000019)13/01/2018 17:13:14 - victor (192.168.1.38)> 230 Logged on
(000019)13/01/2018 17:13:14 - victor (192.168.1.38)> TYPE I
(000019)13/01/2018 17:13:14 - victor (192.168.1.38)> 200 Type set to I
(000019)13/01/2018 17:13:14 - victor (192.168.1.38)> PASV
(000019)13/01/2018 17:13:14 - victor (192.168.1.38)> 227 Entering Passive Mode (192,168,1,41,125,49)
(000019)13/01/2018 17:13:14 - victor (192.168.1.38)> RETR /Informes/572760344M/aaa.pdf
(000019)13/01/2018 17:13:14 - victor (192.168.1.38)> 150 Opening data channel for file download from server of "/Informes/572760344M/aaa.pdf"
(000019)13/01/2018 17:13:14 - victor (192.168.1.38)> 226 Successfully transferred "/Informes/572760344M/aaa.pdf"
(000019)13/01/2018 17:13:48 - victor (192.168.1.38)> disconnected.

問題出在哪里??

你不見了

outputStream.close();

無需完全重寫您的代碼。 您的新代碼只是不必要地重新實現了FTPClient.retrieveFile在內部所做的工作。

即使您選擇使用FTPClient.retrieveFileStream ,請使用Util.copyStream ) 而不是自己實現復制循環。

我已經找到了錯誤,它是在存儲過程中:

改變這一點:

OutputStream outputStream = null;
            boolean success = false;
            outputStream = new BufferedOutputStream(new FileOutputStream(
                    localFile));
            success = ftp.retrieveFile(filename, outputStream);

            if(success) {
                Log.d(LOG_TAG, "success!!");
            } else{
                Log.d(LOG_TAG, "NOOOOT success!!");
            }

對此:

 InputStream myFileStream = null;
            myFileStream = ftp.retrieveFileStream(filename);
            File fileToWrite = new File(params[5]);
            java.io.FileOutputStream fos = new java.io.FileOutputStream("/storage/emulated/0/Download/"+fileToWrite);
            java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
            byte data[] = new byte[1024];
            int x =0;
            while((x=myFileStream.read(data,0,1024))>=0){
                bout.write(data,0,x);
            }
            bout.close();
            myFileStream.close();
            res = ftp.getReplyCode();

完美運行!

暫無
暫無

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

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