gpt4 book ai didi

java.lang.OutOfMemoryError 上传大文件

转载 作者:行者123 更新时间:2023-11-29 08:57:08 25 4
gpt4 key购买 nike

在 Nick Wilson 回答之前:

我正在 Glassfish 3 下开发 Java EE 6 网络应用程序。主要特点是能够上传大文件(1Gb 或更大)。当我尝试上传一个文件(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)

我已经在 Glassfish JVM 选项中增加了内存:

-XX:MaxPermSize=256m
-XX:PermSize=128m
-XX:NewRatio=2
-XX:+UseParallelGC
-Xmx2048m

我尝试了以下代码来上传文件,但都给我同样的错误:

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);
}

 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();

我该如何解决这个问题?

谢谢

在 Nick Wilson 回答之后

我将我的代码更改为:

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);
}

但是我收到了同样的异常。

能够上传大文件(1Gb 或更大)。当我尝试上传一个文件(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)

我已经在 Glassfish JVM 选项中增加了内存:

-XX:MaxPermSize=256m
-XX:PermSize=128m
-XX:NewRatio=2
-XX:+UseParallelGC
-Xmx2048m

我尝试了以下代码来上传文件,但都给我同样的错误:

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);
}

 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();

我该如何解决这个问题?

谢谢

在 Nick Wilson 回答之后

我将我的代码更改为:

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);
}

但是我收到了同样的异常。

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

我不知道怎么解决:-(

最佳答案

您应该能够从上传的文件中流式传输数据,而不是调用 getData:

 public void listener(FileUploadEvent event) throws Exception {
UploadedFile item = event.getUploadedFile();
InputStream is = item.getInputStream();
.....

调用 item.getData() 将导致整个文件加载到内存中。

关于java.lang.OutOfMemoryError 上传大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19655881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com