I understand that I can upload 1 file at a time to AppEngine using POST requests for multi-page / forms. AppEngine also supports downloading multiple files , but you need to do some Jokky JSP stuff to work.
I have an application that requires me to upload some form data, 2 images and 3 text fields. Can this be done through AppEngine? I'm trying to find information about this, but it's hard for me to work with the flexibility that I need. I will store the data in the blob repository / repository.
I need a Java solution.
This is the signature of my POST method:
import javax.ws.rs.Consumes; import javax.ws.rs.POST; @POST @Consumes(MediaType.MULTIPART_FORM_DATA) public void post( @Context HttpServletRequest request, @Context HttpServletResponse response) throws FileUploadException, IOException {}
Copy and paste the Java Servlet if you really need it. The above is the question and the corresponding servlet fragments.
import java.io.BufferedReader; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.util.Iterator; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import org.apache.commons.fileupload.FileItemHeaders; import org.apache.commons.fileupload.FileItemIterator; import org.apache.commons.fileupload.FileItemStream; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.servlet.ServletFileUpload; import com.google.appengine.api.blobstore.BlobstoreService; import com.google.appengine.api.blobstore.BlobstoreServiceFactory; import com.google.appengine.api.files.AppEngineFile; import com.google.appengine.api.files.FileReadChannel; import com.google.appengine.api.files.FileService; import com.google.appengine.api.files.FileServiceFactory; import com.google.appengine.api.files.FileWriteChannel; @Path("/upload") public class FileUploadServlet { private BlobstoreService blobstoreService = BlobstoreServiceFactory .getBlobstoreService(); @POST @Consumes(MediaType.MULTIPART_FORM_DATA) public void post(@Context HttpServletRequest request, @Context HttpServletResponse response) throws FileUploadException, IOException { final ServletFileUpload upload = new ServletFileUpload(); final FileItemIterator fileIter = upload.getItemIterator(request); while (fileIter.hasNext()) { final FileItemStream item = fileIter.next(); String name = item.getName(); String fieldName = item.getFieldName(); String contentType = item.getContentType(); Log.d("Name = " + name); Log.d("Field-Name = " + fieldName); Log.d("Content-Type = " + contentType); FileItemHeaders headers = item.getHeaders(); if(headers != null) { Iterator<String> it = (Iterator<String>)headers.getHeaderNames(); while(it.hasNext()) { String h = it.next(); Log.d(h + " = " + headers.getHeader(h)); } } if (item.isFormField()) {
Full exception:
WARNING: /api/upload java.nio.charset.IllegalCharsetNameException: image/jpeg at java.nio.charset.Charset.checkName(Charset.java:284) at java.nio.charset.Charset.lookup2(Charset.java:458) at java.nio.charset.Charset.lookup(Charset.java:437) at java.nio.charset.Charset.forName(Charset.java:502) at java.nio.channels.Channels.newReader(Channels.java:381) at com.futonredemption.starstarstar.FileUploadServlet.post(FileUploadServlet.java:96) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) blah blah blah
source share