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