我们有一个运行在Linux上的Weblogic服务器,带有多个托管服务器。我关注的受管服务器的最大堆大小为1024MB。该服务器上部署了多个应用程序。这些应用程序之一处理来自REST api的响应,以下载最大250MB的文件。对于大于50 MB的文件,有时没有问题,但有时服务器因OOM错误而崩溃。以下是我的代码:
Client client = Client.create();
WebResource webResource = client.resource(url.toString());
ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus());
}
String output = response.getEntity(String.class);
byte[] fileContent = Base64.decodeBase64(output.getBytes());
if (fileContent != null) {
OutputStream out = null;
try {
res.reset();
out = res.getOutputStream();
res.setContentType(contentType);
res.setHeader("Content-Disposition", "inline; filename=" + fileName + "; size=" + String.valueOf(fileContent.length));
res.setContentLength(fileContent.length);
out.write(fileContent);
} catch (Exception ex) {
e.printStackTrace();
} finally {
out.flush();
out.close();
}
}
由于存在内存问题,我尝试采用InputStream方法。以下是更改后的代码:
Client client = Client.create();
WebResource webResource = client.resource(url.toString());
ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus());
}
InputStream source = response.getEntityInputStream(); // IS THIS OKAY?
if (source!= null) {
OutputStream out = null;
int count = 0;
try {
byte[] buffer = new byte[1024];
int read = 0;
res.reset();
res.setContentType(contentType);
res.setHeader("Content-Disposition", "inline; filename=" + fileName);
out = res.getOutputStream();
while ((read = source.read(buffer)) != -1) {
out.write(buffer, 0, read);
count++;
}
System.out.println("COUNT: " + count);// For a 60MB file, this prints 86000. why?
} catch (Exception ex) {
e.printStackTrace();
}finally{
out.flush();
out.close();
source.close();
}
此代码未在OOM中运行,但文件无法加载/已损坏。问题是否与Base64编码的响应有关,并且我没有正确处理它?如果是,该怎么办?该应用程序在Java 7上运行。我使用InputStream解决OOM的方法好吗?该服务未按块发送响应。我还能采取什么其他方式?我担心仅50 MB的文件就会遇到错误(并非总是如此)。该服务器是远程服务器,由另一组处理。如何检查是否有其他原因导致服务器崩溃?
我使用Base64InputStream解码InputStream响应,并且工作正常。
InputStream stream = response.getEntityInputStream();
Base64InputStream bis = new Base64InputStream(stream);
然后,使用bis写入文件。现在,此方法将在65000左右打印计数变量。