小编use*_*304的帖子

如何在java中使用okhttpclient下载图像文件

我想问一下如何在java中使用okhttpclient下载图像文件,因为我需要下载带有会话的文件这里是正式给出的代码,但我不知道如何使用它作为图像文件下载

private final OkHttpClient client = new OkHttpClient();

  public void run() throws Exception {
    Request request = new Request.Builder()
        .url("http://publicobject.com/helloworld.txt")
        .build();

    Response response = client.newCall(request).execute();
    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

    Headers responseHeaders = response.headers();
    for (int i = 0; i < responseHeaders.size(); i++) {
      System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
    }

    System.out.println(response.body().string());
  }
Run Code Online (Sandbox Code Playgroud)

java image download okhttp

16
推荐指数
2
解决办法
1万
查看次数

标签 统计

download ×1

image ×1

java ×1

okhttp ×1