我有一个REST服务,它向我发送一个大的ISO文件,REST服务中没有问题。 现在我已经编写了一个Web应用程序,它调用其余服务来获取文件,在客户端(Web应用程序)端,我收到一个Out Of memory Exception.Below是我的代码
HttpHeaders headers = new HttpHeaders();//1 Line
headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));//2 Line
headers.set("Content-Type","application/json");//3 Line
headers.set("Cookie", "session=abc");//4 Line
HttpEntity statusEntity=new HttpEntity(headers);//5 Line
String uri_status=new String("http://"+ip+":8080/pcap/file?fileName={name}");//6 Line
ResponseEntity<byte[]>resp_status=rt.exchange(uri_status, HttpMethod.GET, statusEntity, byte[].class,"File5.iso");//7 Line
我在7行收到内存异常,我想我将不得不缓冲并获取部分内容,但不知道如何从服务器获取此文件,文件大小约为500到700 MB。 任何人都可以帮忙。
异常堆栈:
org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.OutOfMemoryError: Java heap space
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:972)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.OutOfMemoryError: Java heap space
java.util.Arrays.copyOf(Arrays.java:3236)
java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:118)
java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:153)
org.springframework.util.FileCopyUtils.copy(FileCopyUtils.java:113)
org.springframework.util.FileCopyUtils.copyToByteArray(FileCopyUtils.java:164)
org.springframework.http.converter.ByteArrayHttpMessageConverter.readInternal(ByteArrayHttpMessageConverter.java:58)
org.springframework.http.converter.ByteArrayHttpMessageConverter.readInternal(ByteArrayHttpMessageConverter.java:1)
org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:153)
org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:81)
org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:627)
org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:1)
org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:454)
org.springframework.web.client.RestTemplate.execute(RestTemplate.java:409)
org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:385)
com.pcap.webapp.HomeController.getPcapFile(HomeController.java:186)
我的服务器端REST服务代码正常工作
@RequestMapping(value = URIConstansts.GET_FILE, produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE}, method = RequestMethod.GET)
public void getFile(@RequestParam(value="fileName", required=false) String fileName,HttpServletRequest request,HttpServletResponse response) throws IOException{
byte[] reportBytes = null;
File result=new File("/home/arpit/Documents/PCAP/dummyPath/"+fileName);
if(result.exists()){
InputStream inputStream = new FileInputStream("/home/arpit/Documents/PCAP/dummyPath/"+fileName);
String type=result.toURL().openConnection().guessContentTypeFromName(fileName);
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
response.setHeader("Content-Type",type);
reportBytes=new byte[100];//New change
OutputStream os=response.getOutputStream();//New change
int read=0;
while((read=inputStream.read(reportBytes))!=-1){
os.write(reportBytes,0,read);
}
os.flush();
os.close();
}
答案 0 :(得分:19)
我是这样做的。基于此Spring Jira issue的提示。
RestTemplate restTemplate // = ...;
// Optional Accept header
RequestCallback requestCallback = request -> request.getHeaders()
.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL));
// Streams the response instead of loading it all in memory
ResponseExtractor<Void> responseExtractor = response -> {
// Here I write the response to a file but do what you like
Path path = Paths.get("some/path");
Files.copy(response.getBody(), path);
return null;
};
restTemplate.execute(URI.create("www.something.com"), HttpMethod.GET, requestCallback, responseExtractor);
从前面提到的Jira问题:
请注意,您不能简单地从提取器返回InputStream,因为在execute方法返回时,底层连接和流已经关闭。
Spring 5引入了WebClient
类,它允许异步(例如非阻塞)http请求。来自doc:
与RestTemplate相比,WebClient是:
- 非阻塞,被动,并支持更高的并发性和更少的硬件资源。
- 提供了一个利用Java 8 lambdas的功能API。
- 支持同步和异步方案。
- 支持从服务器向上或向下流式传输。
答案 1 :(得分:0)
这可以防止将整个请求加载到内存中。
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setBufferRequestBody(false);
RestTemplate rest = new RestTemplate(requestFactory);
对于java.lang.OutOfMemoryError:可以解决Java堆空间,为JVM添加更多内存:
-Xmxn指定内存分配池的最大大小(以字节为单位)。此值必须是1024的倍数,大于2 MB。附加 字母k或K表示千字节,或m或M表示兆字节。 默认值是在运行时根据系统配置选择的。
对于服务器部署,-Xms和-Xmx通常设置为相同的值。 请参阅垃圾收集器人体工程学 http://docs.oracle.com/javase/7/docs/technotes/guides/vm/gc-ergonomics.html
示例:
-Xmx83886080
-Xmx81920k
-Xmx80m
您可能遇到的问题与您尝试执行的请求(下载大文件)并不严格相关,但为该进程分配的内存不足。
答案 2 :(得分:0)
上面的正确答案的更好的版本可能是下面的代码。此方法会将下载请求发送到另一个应用程序或服务,该应用程序或服务充当下载信息的真实来源。
public void download(HttpServletRequest req, HttpServletResponse res, String url)
throws ResourceAccessException, GenericException {
try {
logger.info("url::" + url);
if (restTemplate == null)
logger.info("******* rest template is null***********************");
RequestCallback requestCallback = request -> request.getHeaders()
.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL));
// Streams the response instead of loading it all in memory
ResponseExtractor<ResponseEntity<InputStream>> responseExtractor = response -> {
String contentDisposition = response.getHeaders().getFirst("Content-Disposition");
if (contentDisposition != null) {
// Temporary location for files that will be downloaded from micro service and
// act as final source of download to user
String filePath = "/home/devuser/fileupload/download_temp/" + contentDisposition.split("=")[1];
Path path = Paths.get(filePath);
Files.copy(response.getBody(), path, StandardCopyOption.REPLACE_EXISTING);
// Create a new input stream from temporary location and use it for downloading
InputStream inputStream = new FileInputStream(filePath);
String type = req.getServletContext().getMimeType(filePath);
res.setHeader("Content-Disposition", "attachment; filename=" + contentDisposition.split("=")[1]);
res.setHeader("Content-Type", type);
byte[] outputBytes = new byte[100];
OutputStream os = res.getOutputStream();
int read = 0;
while ((read = inputStream.read(outputBytes)) != -1) {
os.write(outputBytes, 0, read);
}
os.flush();
os.close();
inputStream.close();
}
return null;
};
restTemplate.execute(url, HttpMethod.GET, requestCallback, responseExtractor);
} catch (Exception e) {
logger.info(e.toString());
throw e;
}
}
答案 3 :(得分:0)
正如@bernie所述,您可以使用WebClient来实现这一点:
public void downloadFileUrl( HttpServletResponse response ) throws IOException {
WebClient webClient = WebClient.create();
// Request service to get file data
Flux<DataBuffer> fileDataStream = webClient.get()
.uri( this.fileUrl )
.accept( MediaType.APPLICATION_OCTET_STREAM )
.retrieve()
.bodyToFlux( DataBuffer.class );
// Streams the stream from response instead of loading it all in memory
DataBufferUtils.write( fileDataStream, response.getOutputStream() )
.map( DataBufferUtils::release )
.then()
.block();
}
即使没有Reactive Server堆栈,您仍然可以使用WebClient-Rossen Stoyanchev(Spring Framework团队的成员)在Guide to "Reactive" for Spring MVC Developers演示文稿中对此进行了很好的解释。在这次演讲中,Rossen Stoyanchev提到他们考虑过时淘汰RestTemplate ,但是他们决定将其推迟,但是将来它可能还会发生!
到目前为止,使用WebClient的主要缺点是学习曲线(反应式编程)非常陡峭,但是我认为将来没有避免的方法,因此最好早于后者进行研究。
答案 4 :(得分:-1)
您应该使用多部分文件附件,因此文件流不会加载到内存中。 在此示例中,我使用了使用Apache CXF实现的休息服务。
...
import org.apache.cxf.jaxrs.ext.multipart.Attachment;
...
@Override
@Path("/put")
@Consumes("multipart/form-data")
@Produces({ "application/json" })
@POST
public SyncResponseDTO put( List<Attachment> attachments) {
SyncResponseDTO response = new SyncResponseDTO();
try {
for (Attachment attr : attachments) {
log.debug("get input filestream: " + new Date());
InputStream is = attr.getDataHandler().getInputStream();