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 下載結果正確


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM