码迷,mamicode.com
首页 > 其他好文 > 详细

RestTemplate 超级严重BUG之 restTemplate.getForEntity对于下载文件的地址请求 header不起作用

时间:2019-10-07 13:02:33      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:col   com   for   round   int   new   set   upload   ssl   

错误下载:
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 下载结果正确

RestTemplate 超级严重BUG之 restTemplate.getForEntity对于下载文件的地址请求 header不起作用

标签:col   com   for   round   int   new   set   upload   ssl   

原文地址:https://www.cnblogs.com/coderdxj/p/11629802.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!