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

Posted coderdxj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 下载结果正确

以上是关于RestTemplate 超级严重BUG之 restTemplate.getForEntity对于下载文件的地址请求 header不起作用的主要内容,如果未能解决你的问题,请参考以下文章

JSPatch来更新已上线的App中出现的BUG(超级详细)

JSPatch来更新已上线的App中出现的BUG(超级详细)

记一次RestTemplate消息类型不匹配的BUG定位

记一次RestTemplate消息类型不匹配的BUG定位

记一次RestTemplate消息类型不匹配的BUG定位

spring RestTemplate提交json格式数据