zoukankan      html  css  js  c++  java
  • RestTemplate 超级严重BUG之 restTemplate.getForEntity对于下载文件的地址请求 header不起作用

    错误下载:
    RestTemplate restTemplate=new RestTemplate();
    HttpHeaders httpHeaders=new HttpHeaders();
    httpHeaders.set(HttpHeaders.RANGE,"bytes=" + 0 + "-" + 10);
    org.springframework.http.HttpEntity<byte[]> httpEntity=new org.springframework.http.HttpEntity<>(httpHeaders);
    ResponseEntity<byte[]> resp = restTemplate.getForEntity("http://b-ssl.duitang.com/uploads/item/201804/29/20180429134705_yk5mz.jpeg", byte[].class,httpEntity);
    System.out.println(Thread.currentThread().getName()+":"+resp.getBody().length);
    资源总共:1078729byte 希望下载11byte 结果下载结果为1078729byte 结论:header设置没有起作用

    正确下载:

    RestTemplate restTemplate=new RestTemplate();
    HttpHeaders httpHeaders=new HttpHeaders();
    httpHeaders.set(HttpHeaders.RANGE,"bytes=" + 0 + "-" + 10);
    org.springframework.http.HttpEntity<byte[]> httpEntity=new org.springframework.http.HttpEntity<>(httpHeaders);
    ResponseEntity<byte[]> rsp = restTemplate.exchange("http://b-ssl.duitang.com/uploads/item/201804/29/20180429134705_yk5mz.jpeg", HttpMethod.GET, httpEntity, byte[].class);
    System.out.println(Thread.currentThread().getName()+":"+rsp.getBody().length);

    橙色为唯一不同代码:把getForEntity 换为了exchage 下载结果正确
  • 相关阅读:
    Jersey Politics
    网络流——最小费用最大流
    网络流——最大流Dinic算法
    【洛谷2756】飞行员配对方案问题(二分图匹配,网络流24题)
    状压dp入门
    2018九江市赛
    [CQOI2007]余数求和
    CSAPC2008 skyline
    [ZJOI2009]函数 题解
    由不定方程想到的——数论选讲
  • 原文地址:https://www.cnblogs.com/coderdxj/p/11629802.html
Copyright © 2011-2022 走看看