2017-10-09 18 views
1

Java FTPClient를 사용하여 서버에서 파일을 다운로드하고 있습니다. 파일이 다운로드되면 무결성을 확인한 다음 삭제하고 싶습니다. 다운로드 한 파일의 크기 (바이트)와 서버의 파일 크기 (바이트)를 비교하여 결과를 얻지는 않습니다. 당신이 볼 수FTP 서버에서 다운로드 한 파일의 길이가 서버의 파일 길이와 같지 않습니다.

다음
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"); 
    } 
} 

가 두 번 루프를 실행 한 후 출력 인의 크기

다음
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 방법 : 아래

내 전송 디렉토리에서 추출입니다 다운로드 한 파일은 다양 할 수 있습니다.

 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

아, 알겠습니다. 그럼 알 니탁이 알아 냈어. –

답변

3

.close() 당신의 BufferedOutputStream 전에 쓰기 파일의 크기를 측정하려고 시도합니다.

.close()이 없으면 스트림에 작성한 모든 데이터가 실제로 File 개체를 통해 액세스하는 기본 파일에 기록된다는 보장 (실제로는 반대입니다)이 없습니다.

+0

ok, outputStream2를 어디에서 닫을 것을 권장합니까? – steam1234322

+0

파일 크기를 측정하기 전에 파일을 수정하는 동안 파일의 크기를 확인하는 것이 좋습니다. –

+2

더 정확하게, while 루프 바로 뒤에 있습니다. –