【接下载那一节】通过下载接口获取图片,存到本地

从response中获取到流,写到本地

方法一:直接通过URL,但遇到需要token的地址时,就可以用了(也许是我没找到用法)

public void get2(String urlStr) {
    InputStream inputStream = null;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    FileOutputStream fileOutputStream = null;
    try {
        URL url = new URL(urlStr);
        inputStream = url.openStream(); // 从response中获取二进制流

        byte[] buffer = new byte[1024];
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }

        // 写到本地
        byte[] data = outputStream.toByteArray();
        File imageFile = new File("D:/test.jpg");
        fileOutputStream = new FileOutputStream(imageFile);
        fileOutputStream.write(data);

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (null != inputStream) {
            try {
                inputStream.close();
            } catch (Exception e) {
            }
        }

        if (null != outputStream) {
            try {
                outputStream.close();
            } catch (Exception e) {
            }
        }

        if (null != fileOutputStream) {
            try {
                fileOutputStream.close();
            } catch (Exception e) {
            }
        }
    }
}

方法二:通过http的形式,请求地址,带token和不带token的方法均可使用

public void get1(String urlStr) {
    String token = getToken(urlStr);

    Map<String, String> header = new HashMap<>();
    header.put("X-Auth-Token", token);

    InputStream inputStream = null;
    OutputStream outputStream = null;
    FileOutputStream fileOutputStream = null;
    try {
        Response response = OkhttpUtil.getSync(urlStr, header);
        if (null != response) {
            if (200 == response.code()) {
                inputStream = response.body().byteStream();// 从response中获取二进制流
            }
        }

        byte[] buffer = new byte[1024];
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }

        File file = new File("D:/test.jpg");
        fileOutputStream = new FileOutputStream(file);
        fileOutputStream.write(buffer);

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (null != inputStream) {
            try {
                inputStream.close();
            } catch (Exception e) {
            }
        }

        if (null != outputStream) {
            try {
                outputStream.close();
            } catch (Exception e) {
            }
        }

        if (null != fileOutputStream) {
            try {
                fileOutputStream.close();
            } catch (Exception e) {
            }
        }
    }
}

类OkhttpUtil:依赖okhttp3

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

import java.util.Map;
import java.util.concurrent.TimeUnit;

public class OkhttpUtil {
    private static volatile OkHttpClient okHttpClient;

    // 获取单例客户端
    public static OkHttpClient getInstance() {
        if (okHttpClient == null) {
            synchronized (OkhttpUtil.class) {
                if (null == okHttpClient) {
                    OkHttpClient.Builder builder = new OkHttpClient.Builder().
                            connectTimeout(15, TimeUnit.SECONDS).
                            writeTimeout(20, TimeUnit.SECONDS).
                            readTimeout(20, TimeUnit.SECONDS);
                    okHttpClient = builder.build();
                }
            }
        }

        return okHttpClient;
    }

    public static Response getSync(String url, Map<String, String> header) {
        Response response = null;
        try {
            Request.Builder builder = new Request.Builder();
            if (null != header && !header.isEmpty()) {
                for (Map.Entry<String, String> entry : header.entrySet()) {
                    builder.addHeader(entry.getKey(), entry.getValue());
                }
            }

            Request request = builder.url(url).get().build();
            response = getInstance().newCall(request).execute();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return response;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值