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 下载结果正确
posted @ 2019-10-07 11:19  洞玄巅峰  阅读(3865)  评论(0编辑  收藏  举报