0

I'm using Google App Engine on Eclipse and I want the users to be able to upload documents. I have used the example given on How to upload and store an image with google app engine (java). What should I do to get rid of the error that says "unhandled exception type FileUploadException" on the two lines right after ServletFileUpload upload = ...

Here's the exact code I've used:

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

And 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;
    }
} 
Community
  • 1
  • 1

1 Answers1

0

you dont need to use ServletFileUpload. Use the core blobstore api provided by GAE.

I consider that you are form submitting as in the docs. So in the server,

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

Image image             =   ImagesServiceFactory.makeImageFromBlob(blobKeys.get(0));
Bharath
  • 519
  • 4
  • 7
  • So you are suggesting to completely remove all lines that include ServletFileUpload? @Bharath – 19anamika94 Aug 22 '14 at 16:55
  • yes, i am using Blobstore Api for more than 1 year. It supports multiple file uploads too. – Bharath Aug 22 '14 at 17:07
  • If I get rid of ServletFileUpload upload = new ServletFileUpload(); then upload will be undefined. Not sure what to do as I'm not familiar with this. @Bharath – 19anamika94 Aug 22 '14 at 17:11
  • Those Classes are not needed. The two lines of code in the answer is enough to get the image from the request. In the getUploads method pass the HTTPservletrequest – Bharath Aug 22 '14 at 17:39
  • Do I need to create the method or there is another way to pass the HTTPServletRequest? – 19anamika94 Aug 22 '14 at 18:47
  • okay! I got it to start uploading in a temp file now. Thank you! – 19anamika94 Aug 25 '14 at 23:38