什么是“未处理的异常类型FileUploadException”,我该如何解决这个问题?

时间:2014-08-22 01:48:58

标签: java google-app-engine file-upload

我在Eclipse上使用Google App Engine,我希望用户能够上传文档。我使用了How to upload and store an image with google app engine (java)上给出的示例。我应该怎么做才能摆脱ServletFileUpload上传后两行上出现“未处理异常类型FileUploadException”的错误= ...

这是我用过的确切代码:

    package com.example.test;

    import java.io.*;

    import javax.jdo.PersistenceManager;
    import javax.jdo.PersistenceManagerFactory;
    import javax.servlet.http.*;

    import org.apache.commons.fileupload.FileItemIterator;
    import org.apache.commons.fileupload.FileUploadException;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;
    import org.apache.commons.fileupload.*;
    import org.apache.commons.io.IOUtils;

    import com.google.appengine.api.datastore.Blob;


    @SuppressWarnings("serial")
    public class TestServlet extends HttpServlet {
            public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {                
                // Get the image representation
                ServletFileUpload upload = new ServletFileUpload();
                FileItemIterator iter = upload.getItemIterator(req);
                FileItemStream imageItem = iter.next();
                InputStream imgStream = imageItem.openStream();

                // construct our entity objects
                Blob imageBlob = new Blob(IOUtils.toByteArray(imgStream));
                MyImage myImage = new MyImage(imageItem.getName(), imageBlob);

                // persist image
                PersistenceManager pm = PMF.get().getPersistenceManager();
                pm.makePersistent(myImage);
                pm.close();

                // respond to query
                res.setContentType("text/plain");
                res.getOutputStream().write("OK!".getBytes());
            }
        }

MyImage.java:     package com.example.test;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.persistence.Entity;

import com.google.appengine.api.datastore.Blob;

@Entity
public class MyImage {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;

    @Persistent
    private String name;

    @Persistent
    Blob image;

    public MyImage() { }
    public MyImage(String name, Blob image) {
        this.name = name; 
        this.image = image;
    }

    // JPA getters and setters and empty contructor
    // ...
    public Blob getImage()              { return image; }
    public void setImage(Blob image)    { this.image = image; }
}

和PMF.java:

package com.example.test;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;

public final class PMF {
    private static final PersistenceManagerFactory pmfInstance =
        JDOHelper.getPersistenceManagerFactory("transactions-optional");

    private PMF() {}

    public static PersistenceManagerFactory get() {
        return pmfInstance;
    }
} 

1 个答案:

答案 0 :(得分:0)

您不需要使用ServletFileUpload。使用GAE提供的核心blobstore api。

我认为您是在文档中提交表单。所以在服务器中,

List<BlobKey> blobKeys  =   BlobstoreServiceFactory.getBlobstoreService().getUploads(request).get("file tag's name attribute value");

Image image             =   ImagesServiceFactory.makeImageFromBlob(blobKeys.get(0));