2017-10-09 6 views
1

FTPClientをJavaで使用してサーバーからファイルをダウンロードしています。ファイルがダウンロードされると、完全性をチェックしてから削除したいと思います。 ダウンロードしたファイルのサイズをバイト単位で比較し、サーバー上のファイルをバイト単位で比較していますが、結果は期待どおりではありません。FTPサーバーからダウンロードしたファイルの長さがサーバーのファイルと同じでない

以下

は私の転送ディレクトリからの抽出物である:以下

for (int i = 0; i <= insideDirectory.length - 1; i++) { 
        FTPFile transferFile = insideDirectory[i]; 
        LOGGER.info("Passing file" + folder.getName() + "/" + transferFile.getName()); 

        File downloadFile = new File("https://stackoverflow.com/users/home/example" + i + ".mp4"); 
        OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(downloadFile)); 
        System.out.println(transferFile.getSize()); 
        ftpClient.enterLocalPassiveMode(); 
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
        InputStream inputStream = ftpClient 
          .retrieveFileStream(folder.getName() + "/" + transferFile.getName()); 
        byte[] bytesArray = new byte[4096]; 
        int bytesRead = -1; 
        while ((bytesRead = inputStream.read(bytesArray)) != -1) { 
         outputStream2.write(bytesArray, 0, bytesRead); 
        } 

        Boolean success = ftpClient.completePendingCommand(); 
        if (success) { 
         System.out.println("File #" + i + " has been downloaded successfully."); 
         checkIfExists(downloadFile, transferFile); 
        } 

あなたが見ることができるようにここに私のcheckIfExists方法

public void checkIfExists(File downloadedFile, FTPFile remoteFileToDelete) { 
    Long downloadedLength = downloadedFile.length(); 
    Long remoteLength = remoteFileToDelete.getSize(); 
    if (downloadedFile.length() == remoteFileToDelete.getSize()) { 
     LOGGER.info(downloadedLength + "exists and is the same length as " + remoteLength + ". Let's delete"); 
    } else { 
     LOGGER.info(downloadedLength + "is not the same length as " + remoteLength + ". Let's not delete"); 
    } 
} 

は、二回のループを実行した後のサイズ出力されます。ダウンロードしたファイルは変更できます:

 File #0 has been downloaded successfully. 
    INFO: 7596008is not the same length as 7600840. Let's not delete 
    File #1 has been downloaded successfully. 
    INFO: 6873664is not the same length as 6878544. Let's not delete 
    File #2 has been downloaded successfully. 
    INFO: 7558112is not the same length as 7564744. Let's not delete 
    File #3 has been downloaded successfully. 
    INFO: 8662336is not the same length as 8665108. Let's not delete 

    File #0 has been downloaded successfully. 
    INFO: 7594312is not the same length as 7600840. Let's not delete 
    File #1 has been downloaded successfully. 
    INFO: 6870392is not the same length as 6878544. Let's not delete 
    File #2 has been downloaded successfully. 
    INFO: 7559184is not the same length as 7564744. Let's not delete 
    File #3 has been downloaded successfully. 
    INFO: 8660888is not the same length as 8665108. Let's not delete 
+0

どうすれば確認できますか? – steam1234322

+0

下部の出力が十分ではありませんか?両方のファイルのサイズをバイト単位で比較します。 – steam1234322

+0

ああ、わかります。それからAlnitakはそれを理解した。 –

答えて

3

.close()BufferedOutputStreamを入力してから、書き込まれたファイルのサイズを測定してください。

.close()がないと、ストリームに書き込んだすべてのデータが実際にFileオブジェクトを介してアクセスする基礎となるファイルに書き込まれるという保証はありません。

+0

OK、outputStream2をどこで閉じることをお勧めしますか? – steam1234322

+0

ファイルのサイズを測定する前に。それを修正する過程でファイルのサイズをチェックするのは良いことですか? –

+2

より正確には、 'while'ループの直後です。 –

関連する問題