6

I use Apache Commons FileUpload in a java server-side app that has a html form with fields :

  1. A destination fied that will be filled with email address of the destination mailbox
  2. A message text with a message of the sender
  3. A < input type=file ... field for uploading a photo. I can receive uploaded file (as a stream) but how

This app I want to upload on GAE . I can receive uploaded file (as a stream , using org.apache.commons.fileupload.FileItemStream).

I want to receive too input textfields (i.e. 1) and 2)) - completed by the user of app)

I want to access these using org.apache.commons.fileupload.FileItem but I receive java.rmi.server.UID is a restricted class

2
  • Writting file on GAE is not allowed. Commented Apr 21, 2011 at 11:27
  • @gigadot Who said anything about writing files? Commented Apr 22, 2011 at 5:52

2 Answers 2

10

You should use the FileItemIterator of the Apache Commons FileUpload.

import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import java.io.InputStream;
..
public void doPost(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
    try {
      ServletFileUpload upload = new ServletFileUpload();
      res.setContentType("text/plain");

      FileItemIterator iterator = upload.getItemIterator(req);
      while (iterator.hasNext()) {
        FileItemStream item = iterator.next();
        String name = item.getFieldName();
        InputStream stream = item.openStream();

        if (item.isFormField()) {
          //regular form field
          resp.getWriter().println(("Form:" + name + " : " + Streams.asString(stream));
        } else {
          //fileform field 
          resp.getWriter().println(("File:" +name + " : " + item.getName());
        }

      }
    } catch (Exception ex) {
      throw new ServletException(ex);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I was using a FileItemIterator... List<FileItem> fileItemsList = uploader.parseRequest(request); Iterator<FileItem> fileItemsIterator = fileItemsList.iterator(); ... but this was throwing a NullPointer at org.apache.commons.fileupload.disk.DiskFileSystem.getTempFil‌​e because there is no file system... So your code that uses FileItemStream instead works fine on GAE... Thanks lots for posting
1

Take a look at this: Google App Engine and FileUpload

You can't write directly to the filesystem in GAE, but take a look at GAEVFS, which uses the datastore and memcache to emulate a filesystem.

2 Comments

I do not want to make use of persistence on Google cloud , I want to use Streaming futures from FileUpload. I have already sent pictures but having 1) and 2) hard-coded in the servlet . But I do not how to get these 2 field dynamically ie typed by user of app.
Oops ment to comment on the answer above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.