[英]java.lang.OutOfMemoryError uploading big files
Before Nick Wilson answer: 在尼克·威尔逊回答之前:
I'm developing a Java EE 6 web application under Glassfish 3. The main characteristic is to be able to upload big files (1Gb ore more bigger). 我正在Glassglass 3下开发Java EE 6 Web应用程序。主要特征是能够上传大文件(更大1Gb或更大)。 When I try to upload a file (400MB of size) I receive the java.lang.OutOfMemoryError exception: 当我尝试上传文件(大小为400MB)时,收到java.lang.OutOfMemoryError异常:
Caused by: java.lang.OutOfMemoryError
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:272)
at com.google.common.io.ByteStreams.read(ByteStreams.java:899)
at com.google.common.io.ByteStreams.readFully(ByteStreams.java:733)
at com.google.common.io.ByteStreams.readFully(ByteStreams.java:714)
at org.richfaces.request.BaseUploadedFile.getData(BaseUploadedFile.java:68)
at EJB.FileUploadBean.listener(FileUploadBean.java:147)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.sun.el.parser.AstValue.invoke(AstValue.java:254)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at org.richfaces.event.MethodExpressionEventListener.processEvent(MethodExpressionEventListener.java:125)
I already incremented the memory in Glassfish JVM Options: 我已经在Glassfish JVM选项中增加了内存:
-XX:MaxPermSize=256m
-XX:PermSize=128m
-XX:NewRatio=2
-XX:+UseParallelGC
-Xmx2048m
I tried the following code to upload file, but both give me the same error: 我尝试使用以下代码上传文件,但都给了我相同的错误:
InputStream filecontent = null;
int read = 0;
final byte[] bytes = new byte[1024];
OutputStream fop = null;
File fout;
if (file.getLength() > 0) {
try {
fout = new File(filename);
fop = new FileOutputStream(fout);
filecontent = item.getInputStream();
if (!fout.exists()) {
fout.createNewFile();
}
while ((read = filecontent.read(bytes)) != -1) {
fop.write(bytes, 0, read);
}
and 和
FileOutputStream fop = null;
File fout;
if (file.getLength() > 0) {
try {
fout = new File(filename);
fop = new FileOutputStream(fout);
if (!fout.exists()) {
fout.createNewFile();
}
fop.write(item.getData());
fop.flush();
fop.close();
How can I solve the problem? 我该如何解决这个问题?
THanks 谢谢
After Nick Wilson answer 在尼克·威尔逊回答之后
I changed my code to: 我将代码更改为:
public void listener(FileUploadEvent event) throws Exception {
ufile = new UploadFile();
InputStream filecontent = null;
UploadedFile item = event.getUploadedFile();
if (item instanceof UploadedFile25) {
filecontent = ((UploadedFile25) item).getInputStream();
} else {
filecontent = item.getInputStream();
}
UploadedText file = new UploadedText();
file.setLength(item.getData().length);
file.setName(item.getName());
Date date = new Date();
String epoch = fDir + String.valueOf(date.getTime());
File fPath = new File(epoch);
fPath.mkdir();
//file.setData(item.getData());
files.add(file);
String filename = epoch + '/' + item.getName();
int read = 0;
final byte[] bytes = new byte[1024];
OutputStream fop = null;
File fout;
if (file.getLength() > 0) {
try {
fout = new File(filename);
fop = new FileOutputStream(fout);
if (!fout.exists()) {
fout.createNewFile();
}
while ((read = filecontent.read(bytes)) != -1) {
fop.write(bytes, 0, read);
}
But I receive the same exception. 但是我收到同样的例外。
to be able to upload big files (1Gb ore more bigger). 就能上传大文件(更大1Gb或更大)。 When I try to upload a file (400MB of size) I receive the java.lang.OutOfMemoryError exception: 当我尝试上传文件(大小为400MB)时,收到java.lang.OutOfMemoryError异常:
Caused by: java.lang.OutOfMemoryError
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:272)
at com.google.common.io.ByteStreams.read(ByteStreams.java:899)
at com.google.common.io.ByteStreams.readFully(ByteStreams.java:733)
at com.google.common.io.ByteStreams.readFully(ByteStreams.java:714)
at org.richfaces.request.BaseUploadedFile.getData(BaseUploadedFile.java:68)
at EJB.FileUploadBean.listener(FileUploadBean.java:147)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.sun.el.parser.AstValue.invoke(AstValue.java:254)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at org.richfaces.event.MethodExpressionEventListener.processEvent(MethodExpressionEventListener.java:125)
I already incremented the memory in Glassfish JVM Options: 我已经在Glassfish JVM选项中增加了内存:
-XX:MaxPermSize=256m
-XX:PermSize=128m
-XX:NewRatio=2
-XX:+UseParallelGC
-Xmx2048m
I tried the following code to upload file, but both give me the same error: 我尝试使用以下代码上传文件,但都给了我相同的错误:
InputStream filecontent = null;
int read = 0;
final byte[] bytes = new byte[1024];
OutputStream fop = null;
File fout;
if (file.getLength() > 0) {
try {
fout = new File(filename);
fop = new FileOutputStream(fout);
filecontent = item.getInputStream();
if (!fout.exists()) {
fout.createNewFile();
}
while ((read = filecontent.read(bytes)) != -1) {
fop.write(bytes, 0, read);
}
and 和
FileOutputStream fop = null;
File fout;
if (file.getLength() > 0) {
try {
fout = new File(filename);
fop = new FileOutputStream(fout);
if (!fout.exists()) {
fout.createNewFile();
}
fop.write(item.getData());
fop.flush();
fop.close();
How can I solve the problem? 我该如何解决这个问题?
THanks 谢谢
After Nick Wilson answer 在尼克·威尔逊回答之后
I changed my code to: 我将代码更改为:
public void listener(FileUploadEvent event) throws Exception {
ufile = new UploadFile();
InputStream filecontent = null;
UploadedFile item = event.getUploadedFile();
if (item instanceof UploadedFile25) {
filecontent = ((UploadedFile25) item).getInputStream();
} else {
filecontent = item.getInputStream();
}
UploadedText file = new UploadedText();
file.setLength(item.getData().length);
file.setName(item.getName());
Date date = new Date();
String epoch = fDir + String.valueOf(date.getTime());
File fPath = new File(epoch);
fPath.mkdir();
//file.setData(item.getData());
files.add(file);
String filename = epoch + '/' + item.getName();
int read = 0;
final byte[] bytes = new byte[1024];
OutputStream fop = null;
File fout;
if (file.getLength() > 0) {
try {
fout = new File(filename);
fop = new FileOutputStream(fout);
if (!fout.exists()) {
fout.createNewFile();
}
while ((read = filecontent.read(bytes)) != -1) {
fop.write(bytes, 0, read);
}
But I receive the same exception. 但是我收到同样的例外。
Caused by: java.lang.OutOfMemoryError
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:272)
at com.google.common.io.ByteStreams.read(ByteStreams.java:899)
at com.google.common.io.ByteStreams.readFully(ByteStreams.java:733)
at com.google.common.io.ByteStreams.readFully(ByteStreams.java:714)
at org.richfaces.request.BaseUploadedFile.getData(BaseUploadedFile.java:68)
at EJB.FileUploadBean.listener(FileUploadBean.java:157)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.sun.el.parser.AstValue.invoke(AstValue.java:254)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at org.richfaces.event.MethodExpressionEventListener.processEvent(MethodExpressionEventListener.java:125)
... 38 more
I don't know how to solve :-( 我不知道如何解决:-(
您需要确保已给JVM适当的堆大小,检查配置,并确保-Xms设置为512,但-Xmx值比文件大小大得多,尤其是在加载整个文件时进入记忆。
You should be able to stream the data from the uploaded file rather than calling getData
: 您应该能够从上传的文件中流式传输数据,而不是调用getData
:
public void listener(FileUploadEvent event) throws Exception {
UploadedFile item = event.getUploadedFile();
InputStream is = item.getInputStream();
.....
Calls to item.getData()
will cause the whole file to be loaded into memory. 调用item.getData()
将导致整个文件加载到内存中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.