我写了一个方法,通过rest web服务将文件作为字节数组发送,在客户端上,我希望将此文件作为流接收(客户端是一个使用java代码的matlab应用程序)。当我发送的文件也超过300MB时,我在matlab上收到堆错误,因为在eclipse中堆内存设置为356MB,而不是1024MB。有可能对接收到的ResponseEntity使用流,以便一次存储每个字节?或者我必须将Matlab堆内存增加到1024?。我在服务器上有这个代码:
@Override
@RequestMapping(value = "/file", method = RequestMethod.GET)
public ResponseEntity<byte[]> getAcquisition(HttpServletResponse resp,@RequestParam(value="filePath", required = true) String filePath){
final HttpHeaders headers = new HttpHeaders();
OutputStream outputStream = null;
File toServeUp= null;
try{
toServeUp=new File(filePath);
if (!toServeUp.exists()){
headers.setContentType(MediaType.TEXT_PLAIN);
LOG.error("Threw exception in MatlabClientControllerImpl::getAcquisition : File doesn't exists!!");
String message = "ERROR: Could not retrieve file on server, check the path!";
return new ResponseEntity<byte[]>(message.getBytes(Charset.forName("UTF-8")), headers, HttpStatus.OK);
}else{
try(InputStream inputStream = new FileInputStream(toServeUp);) {
resp.setContentType("application/octet-stream");
resp.setHeader("Content-Disposition", "attachment; filename=""+toServeUp.getName()+""");
Long fileSize = toServeUp.length();
resp.setContentLength(fileSize.intValue());
outputStream = resp.getOutputStream();
byte[] buffer = new byte[1024];
int read = 0;
while ((read = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, read);
}
headers.setContentType(MediaType.TEXT_PLAIN);
return new ResponseEntity<byte[]>("ok".getBytes(), headers, HttpStatus.OK);
}
catch (Exception e) {
headers.setContentType(MediaType.TEXT_PLAIN);
ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
LOG.error("Threw exception in MatlabClientControllerImpl::getAcquisition :" + errorResponse.getStacktrace());
String message = "ERROR: Could not send file!";
return new ResponseEntity<byte[]>(message.getBytes(("UTF-8")), headers, HttpStatus.OK);
}finally{
//close the streams to prevent memory leaks
try {
if (outputStream!=null){
outputStream.flush();
outputStream.close();
}
} catch (IOException e) {
headers.setContentType(MediaType.TEXT_PLAIN);
ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
LOG.error("Threw exception in MatlabClientControllerImpl::getAcquisition :" + errorResponse.getStacktrace());
String message = "ERROR: Could not close stream.!";
return new ResponseEntity<byte[]>(message.getBytes("UTF-8"), headers, HttpStatus.OK);
}
}
}
}catch(Exception e){
headers.setContentType(MediaType.TEXT_PLAIN);
ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
LOG.error("Threw exception in MatlabClientControllerImpl::getAcquisition :" + errorResponse.getStacktrace());
String message = "ERROR: Error in the path, check it!";
return new ResponseEntity<byte[]>(message.getBytes(), headers, HttpStatus.OK);
}
}
在客户端上我有:
@Override
public Response getFile(String serverIp, String toStorePath, String filePath){
ResponseEntity<byte[]> responseEntity = null;
try{
RestTemplate restTemplate = new RestTemplate();
responseEntity = restTemplate.getForEntity(serverIp + "ATS/client/file/?filePath={filePath}", byte[].class, filePath);
if (MediaType.TEXT_PLAIN.toString().equals(responseEntity.getHeaders().getContentType().toString()))
return new Response(false, false, new String(responseEntity.getBody(),Charset.forName("UTF-8")), null);
else{
Path p = Paths.get(filePath);
String fileName = p.getFileName().toString();
FileOutputStream fos = new FileOutputStream(toStorePath+"\"+ fileName);
fos.write(responseEntity.getBody());
fos.close();
return new Response(true, true, "Your file has been downloaded!", null);
}
}catch(Exception e){
ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
return new Response(false, false, "Error on the client side!" , errorResponse);
}
}
实际上,所有的文件都在responseEntity
中,它抛出堆异常。谢谢,向致以最良好的问候
您可以使用setBufferRequestBody(false)依次生成StreamingClientHttpRequest,该请求在调用getBody()时打开连接。
final RestTemplate restTemplate = new RestTemplate();
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setBufferRequestBody(false);
restTemplate.setRequestFactory(requestFactory);
在那之后,我认为只要使用FileSystemResource作为请求主体就可以做正确的事情。