gpt4 book ai didi

java - 使用 OkHTTP 客户端下载二进制文件已损坏

转载 作者:行者123 更新时间:2023-11-30 06:03:19 26 4
gpt4 key购买 nike

我正在尝试使用 OkHttp 下载二进制文件,并取得进展。
BUFFER_SIZE1 时,文件即可正确下载。
但是,当我将 BUFFER_SIZE 设置为 1024 时,文件会损坏。
BUFFER_SIZE 设置为 1 文件需要很长时间才能下载

下面是代码片段:

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class DownloadTest {

public static String url = "https://cdn.pixabay.com/photo/2017/02/06/12/34/reptile-2042906_1280.jpg";

public static void main(String[] args) throws Exception {
OkHttpClient client = new OkHttpClient();
Call call = client.newCall(new Request.Builder().url(url).get().build());
Response response = call.execute();

System.out.println("" + response.headers().toString());
System.out.println("" + response.body().contentLength());
InputStream inputStream = response.body().byteStream();

float contentLength = (float) response.body().contentLength();

OutputStream fileOutputStream = new FileOutputStream(new File("myfile.jpg"));

System.out.println("writing file " + contentLength);

float downloaded = 0;

/**
* IF BUFFER_SIZE IS 1 file is downloaded properly
* if BUFFER_SIZE is 1024 file is corrupted
* open the downloaded image to test
*/
//byte[] BUFFER_SIZE = new byte[1]; //Proper Download
byte[] BUFFER_SIZE = new byte[1024]; //File Corrupt



while (true) {
int byteRead = inputStream.read(BUFFER_SIZE);
if (byteRead == -1) {
break;
}

downloaded += byteRead;

fileOutputStream.write(BUFFER_SIZE);

System.out.println(" " + downloaded + "/" + contentLength + " = " + ((downloaded / contentLength) * 100));

}
fileOutputStream.flush();
fileOutputStream.close();

System.out.println("file closed");
}

}

最佳答案

如果您的 BUFFER_SIZE 在上次读取时未满,那么您将在文件中写入错误的数据:

你有

fileOutputStream.write(BUFFER_SIZE);

你应该有:

fileOutputStream.write(BUFFER_SIZE, 0, byteRead);

EDIT1:我还建议替换这部分代码:

while (true) {
int byteRead = inputStream.read(BUFFER_SIZE);
if (byteRead == -1) {
break;
}

采用更好的方法:

int byteRead;
while ( (byteRead = inputStream.read(BUFFER_SIZE)) > 0 ) {

关于java - 使用 OkHTTP 客户端下载二进制文件已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51846512/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com