- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 OkHttp
下载二进制文件,并取得进展。
当 BUFFER_SIZE
为 1
时,文件即可正确下载。
但是,当我将 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/
我正在实现 Server-Sent Events使用 OkHttp 的库。服务器发送事件的工作原理是与服务器保持开放的 HTTP 连接,在该服务器上,“事件”可以流式传输回客户端。连接只会在错误或客户
我需要用 OkHttp 重现错误,这样我就可以提交错误或在 StackOverflow 上提问。 无需大量设置即可执行此操作的最简单方法是什么? 我读过 https://stackoverflow.c
我需要用 OkHttp 重现错误,这样我就可以提交错误或在 StackOverflow 上提问。 无需大量设置即可执行此操作的最简单方法是什么? 我读过 https://stackoverflow.c
我正在尝试使用 OkHttp 与 HTTP/2 服务器进行通信客户端。 添加到 Maven POM: com.squareup.okhttp3 okhttp 3.2.0 这是我的
我在使用我的应用程序时不断在 Logcat 中看到以下日志行: 19098-19147/ W/OkHttpClient: A connection to was leaked. Did you fo
我的团队正受此困扰issue使用 slack 集成来上传文件,因此根据该问题中的评论,我想限制我们的 Kotlin 实现中的请求。 我正在尝试集成 Okio Throttler在 OkHttp 拦截器
如何验证 okhttp 是否成功协商了 http/2 而没有使用 https/1.1?我可以检查回复中的一些信息吗? 谢谢, -托尼 最佳答案 仅供引用,因为 okhttp 3.0 左右 OkHttp
我想提出一个请求并强制它使用 Protocol.HTTP_2。 我尝试了下面的代码: import okhttp3.{OkHttpClient, Protocol, Request} import s
我不太熟悉 OkHttp 库,我对它应该如何用于异步请求有一些疑问。 对于 Android 编程,我将 android 库用于 http,并将 asyncTask 用于某些请求。 但是因为我想尝试 O
我在我的项目中使用 Retrofit-1.6.0 + okhttp-2.0.0-RC1 。当我运行项目时出现错误。 java.lang.RuntimeException: Retrofit detec
OkHttpClient 的默认行为是将 keep-alive 设置为 true。对于我们的特定用例,我需要将其设置为 false。 我没有在 Java 源代码中找到任何设置来执行此操作。 有人遇到过
我试图让 OkHttp 在我的 Android Studio 项目中工作,我创建了一个类并从这里粘贴以下代码:https://github.com/square/okhttp/blob/master/
我注意到 OkHttp 在 Android 5.0+ 中通过 ALPN 支持 h2。 来自 some article ,我了解到我们可以对 http/2 请求使用 NPN 协商。 我可以通过 NPN
我知道,Retrofit 在内部使用 OkHttp。但是,我可以看到一些开发人员提供了以下方法的应用 return new Retrofit.Builder() .baseUrl(Bu
我正在努力让 Android 上的 OkHttpClient 使用自定义证书发出 HTTPS 请求,同时绑定(bind)到特定网络接口(interface)的本地地址。我目前对此的尝试使用以下 OkH
我有一个用于 https 连接演示的 native android 应用程序,我遇到了这样的异常:: 我的连接代码很简单: URL url = new URL("https://192.168.1.5
我正在重写 EventListener 中的 dnsEnd() 函数: @Override public void dnsEnd(Call call, String domainName, L
我有okhttp的android多项目设置,作为多个项目的一部分 在项目A中,okhttp包含在源代码中。 在项目B中,okhttp带有库依赖项的一部分。 构建时,出现以下错误。 Execution
我正在尝试使用pesapal向我的应用程序添加付款但遇到问题,我将它们的依赖实现'com.pesapal.android:pesapalandroid:1.0.5'添加到我的应用程序中,从那时起我就一
我只是尝试使用 OkHttp但是当我将我的 gradle 文件与 Android Studio 同步时,我遇到了以下问题: Error:Failed to find: com.squareup.okh
我是一名优秀的程序员,十分优秀!