Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从服务器流式传输文件时,磁盘上的文件大小大于读取的总字节数,即';发生什么事了?_Java_File Io_Inputstream_Outputstream_Fileoutputstream - Fatal编程技术网

Java 从服务器流式传输文件时,磁盘上的文件大小大于读取的总字节数,即';发生什么事了?

Java 从服务器流式传输文件时,磁盘上的文件大小大于读取的总字节数,即';发生什么事了?,java,file-io,inputstream,outputstream,fileoutputstream,Java,File Io,Inputstream,Outputstream,Fileoutputstream,我正在从artifactory读取一个二进制文件。根据artifactory,文件大小为34952058字节,读取完成后记录的totalBytes计数器也为34952058字节。但是磁盘上的文件大小是39426048字节。发生什么事?? 我尝试了BufferedOutputStream,FileOutputStream和OutputStream。 每次的结果都一样。我错过了什么? 这就是我目前最新的代码: try { URL url = new URL(fw.ge

我正在从artifactory读取一个二进制文件。根据artifactory,文件大小为34952058字节,读取完成后记录的
totalBytes
计数器也为34952058字节。但是磁盘上的文件大小是39426048字节。发生什么事?? 我尝试了
BufferedOutputStream
FileOutputStream
OutputStream
。 每次的结果都一样。我错过了什么?
这就是我目前最新的代码:

try {
                URL url = new URL(fw.getArtifactoryUrl());
                URLConnection connection = url.openConnection();
                in = connection.getInputStream();
                File folder = utils.getFirmwareFolder(null, FirmwareUtils.FIRMWARE_LATEST, true);
                StringBuilder builder = new StringBuilder(folder.toString());
                builder.append("/").append(fw.getFileName());
                Path filePath = Paths.get(builder.toString());
                OutputStream out = Files.newOutputStream(filePath);

                int read = 0;
                int totalBytes = 0;

                while ((read = in.read(bytes)) > 0) {
                    totalBytes += read;
                    out.write(bytes);
                    out.flush();
                }
                logger.info("Total bytes read: " + totalBytes);
                in.close();
                out.close();

<<< more code >>>
试试看{
URL=新URL(fw.getArtifactoryUrl());
URLConnection=url.openConnection();
in=connection.getInputStream();
File folder=utils.getFirmwareFolder(null,FirmwareUtils.FIRMWARE\u-LATEST,true);
StringBuilder=新的StringBuilder(folder.toString());
append(“/”).append(fw.getFileName());
Path filePath=Path.get(builder.toString());
OutputStream out=Files.newOutputStream(文件路径);
int read=0;
int totalBytes=0;
而((读取=in.read(字节))>0){
totalBytes+=读取;
out.write(字节);
out.flush();
}
logger.info(“读取的总字节数:”+totalBytes);
in.close();
out.close();
>

您的代码读取正确,但写入错误

while ((read = in.read(bytes)) > 0) { // Read amount of bytes
    totalBytes += read;  // Add the correct amount of bytes read to total
    out.write(bytes);    // Write the whole array, no matter how much we read
    out.flush();         // Completely unnecessary, can harm performance
}

您需要
输出。write(bytes,0,read)
只写入已读取的字节,而不是整个缓冲区。

您的代码读取正确,但写入错误

while ((read = in.read(bytes)) > 0) { // Read amount of bytes
    totalBytes += read;  // Add the correct amount of bytes read to total
    out.write(bytes);    // Write the whole array, no matter how much we read
    out.flush();         // Completely unnecessary, can harm performance
}

您需要
输出。写入(字节,0,读取)
只写入您读取的字节,而不是整个缓冲区。

谢谢!这是一个愚蠢的错误。我似乎必须等待一段时间才能接受您的答案。谢谢!这是一个愚蠢的错误。我似乎必须等待一段时间才能接受您的答案。