Java 我从FTP服务器下载的文件与服务器上的文件长度不同

Java 我从FTP服务器下载的文件与服务器上的文件长度不同,java,ftp,inputstream,outputstream,ftp-client,Java,Ftp,Inputstream,Outputstream,Ftp Client,我使用Java中的FTPClient从服务器下载文件。下载文件时,我想检查它的完整性,然后删除它。 我是通过比较下载文件的大小(以字节为单位)和服务器上文件的大小(以字节为单位)来实现这一点的,但是结果并不像预期的那样 下面是我的传输目录的摘录: for (int i = 0; i <= insideDirectory.length - 1; i++) { FTPFile transferFile = insideDirectory[i];

我使用Java中的FTPClient从服务器下载文件。下载文件时,我想检查它的完整性,然后删除它。 我是通过比较下载文件的大小(以字节为单位)和服务器上文件的大小(以字节为单位)来实现这一点的,但是结果并不像预期的那样

下面是我的传输目录的摘录:

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("/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);
                    }
以下是两次运行循环后的输出,如您所见,下载文件的大小可能会有所不同:

     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
.close()
在尝试测量写入文件的大小之前,请先执行
BufferedOutputStream


如果没有
.close()
,就无法保证(事实恰恰相反)您写入流的所有数据实际上都已写入您通过
file
对象访问的底层文件。

我该如何检查?底部的输出是否不足?它比较了两个文件的大小(以字节为单位),我明白了。然后Alnitak已经弄明白了。在你测量文件大小之前。在您仍在修改文件的过程中检查文件的大小有什么好处?更准确地说,就在
while
循环之后。@steam1234322“在您尝试测量大小之前”;-)@好主意。这非常有效,我可以问一下为什么关闭流的位置会影响文件大小吗?
     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