3

ニック・ウィルソンが答える前に:

Glassfish 3 で Java EE 6 Web アプリケーションを開発しています。主な特徴は、大きなファイル (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();

どうすれば問題を解決できますか?

ありがとう

ニック・ウィルソンの回答後

コードを次のように変更しました。

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

どうすれば問題を解決できますか?

ありがとう

ニック・ウィルソンの回答後

コードを次のように変更しました。

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

解き方がわかりません(-_-;)

4

2 に答える 2

1

を呼び出すのではなく、アップロードされたファイルからデータをストリーミングできるはずですgetData

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

を呼び出すとitem.getData()、ファイル全体がメモリにロードされます。

于 2013-10-29T11:33:19.207 に答える
0

JVM に適切なヒープ サイズを指定したことを確認し、構成を確認して、-Xms が 512 に設定されていることを確認する必要がありますが、特にファイル全体をロードする場合は、-Xmx 値がファイル サイズよりもはるかに大きくなっています。メモリに。

于 2013-10-29T10:49:21.053 に答える