从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; } }