Java 如何将大文件下载到字节数组中
我正在尝试使用spring Rest模板下载大文件:Java 如何将大文件下载到字节数组中,java,spring,spring-boot,resttemplate,Java,Spring,Spring Boot,Resttemplate,我正在尝试使用spring Rest模板下载大文件: private Result download(Long id) throws Exception { Result result = new Result(); try { RestTemplate restTemplate = restTemplateFactory.getRestTemplate(); HttpHeaders headers = new HttpHeaders();
private Result download(Long id) throws Exception {
Result result = new Result();
try {
RestTemplate restTemplate = restTemplateFactory.getRestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.set(AUTHORIZATION, authorizationBL.getAccessToken().getAccessToken());
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(null, headers);
Map<String, Long> valuesMap = new HashMap<String, Long>(1);
valuesMap.put(ID, id);
ResponseEntity<byte[]> response = restTemplate.exchange(downloadURL, HttpMethod.GET, request, byte[].class);
byte[] stream = response.getBody();
result.setData(stream);
result.setStatus(Result.Status.SUCCESS);
} catch (Exception e) {
throw e;
}
return result;
}
我将堆大小增加到10g,但这也没有帮助 将文件保存到磁盘,而不是下载到字节数组中。然后,您可以从程序中任何需要的地方读取它。例如,将方法更改为
/**
* returns a File corresponding to the tmp file download for this resource
*/
private File download(Long id) throws IOException {
File tmp = null;
try {
RestTemplate restTemplate = restTemplateFactory.getRestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.set(AUTHORIZATION, authorizationBL.getAccessToken().getAccessToken());
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(null, headers);
ResponseEntity<byte[]> response = restTemplate.exchange(downloadURL, HttpMethod.GET, request, byte[].class);
if (response.getStatusCode() == HttpStatus.OK) {
tmp = Files.createTempFile("yourprefix", "yoursuffix");
Files.write(tmp.toPath(), response.getBody());
} else {
// TODO handle this case
}
} catch (Exception e) {
throw new IOException("An exception occurred while downloading", e);
}
return tmp;
}
/**
*返回与此资源的tmp文件下载对应的文件
*/
私有文件下载(长id)引发IOException{
文件tmp=null;
试一试{
RestTemplate RestTemplate=restTemplateFactory.getRestTemplate();
HttpHeaders=新的HttpHeaders();
headers.setContentType(MediaType.APPLICATION\u OCTET\u STREAM);
headers.set(AUTHORIZATION,authorizationBL.getAccessToken().getAccessToken());
HttpEntity请求=新的HttpEntity(空,标题);
ResponseEntity response=restemplate.exchange(下载URL,HttpMethod.GET,请求,字节[].class);
if(response.getStatusCode()==HttpStatus.OK){
tmp=Files.createTempFile(“yourprefix”、“yoursuffix”);
write(tmp.toPath(),response.getBody());
}否则{
//该怎么办
}
}捕获(例外e){
抛出新IOException(“下载时发生异常”,e);
}
返回tmp;
}
一些意见:
值映射未在任何位置使用(也已删除)
结果
也已删除我可以使用stream来解决这个问题,而不是将文件存储在字节[]中&使用
IOUtils.copyragle(inputStream,outputStream)
我需要将大文件转换为字节数组以进行进一步处理。进一步处理意味着什么?您真的应该考虑另一种方法来实现您的业务需求。在字节数组中存储如此大量的数据不是一个好主意。如果你提供了你最初的目标,有人可能会帮助你找到一个强有力的方法来实现这一目标。i、 e.您可以将其保存到文件以供进一步处理。不鼓励抛出异常。最好将其更改为IOException
,同时返回tmp代码>无效,tmp
仅在if块内可用
/**
* returns a File corresponding to the tmp file download for this resource
*/
private File download(Long id) throws IOException {
File tmp = null;
try {
RestTemplate restTemplate = restTemplateFactory.getRestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.set(AUTHORIZATION, authorizationBL.getAccessToken().getAccessToken());
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(null, headers);
ResponseEntity<byte[]> response = restTemplate.exchange(downloadURL, HttpMethod.GET, request, byte[].class);
if (response.getStatusCode() == HttpStatus.OK) {
tmp = Files.createTempFile("yourprefix", "yoursuffix");
Files.write(tmp.toPath(), response.getBody());
} else {
// TODO handle this case
}
} catch (Exception e) {
throw new IOException("An exception occurred while downloading", e);
}
return tmp;
}