在1个请求中将多个文件从Android上传到App Engine

时间:2012-06-08 07:30:14

标签: java android google-app-engine servlets blobstore

据我所知,我可以使用multipart / form POST请求一次将1个文件上传到AppEngine。 AppEngine也支持uploading multiple files,但你必须做一些运行的JSP东西才能工作。

我有一个应用程序,要求我上传一些表单数据,2个图像和3个文本字段。这可以通过AppEngine完成吗?我一直在努力寻找有关这方面的信息,但这很难解决我需要的灵活性。我将把数据存储在blob存储/数据存储中。

我需要一个Java解决方案。

这是我的POST方法的签名:

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

如果确实需要,请复制并粘贴Java Servlet。以上是问题和相关的servlet片段。

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()) {
                // Nothing
            } else {
                RawImageData data = new RawImageData();
                data.load(item.openStream());
                            // RawImageData reads the stream and stores it into a large byte[] called data.imageData
                ByteBuffer bb = ByteBuffer.wrap(data.imageData);
                FileService fs = FileServiceFactory.getFileService();
                AppEngineFile file = fs.createNewBlobFile(contentType);
                FileWriteChannel write = fs.openWriteChannel(file, true);

                write.write(bb);
                write.closeFinally();
                String path = file.getFullPath();
                Log.d(path);

                // Later, read from the file using the file API
                boolean lock = false; // Let other people read at the same time
                FileReadChannel readChannel = fs.openReadChannel(file,
                        false);
                            // CRASHES WITH java.nio.charset.IllegalCharsetNameException: image/jpeg
                            // contentType = "image/jpeg"
                // Again, different standard Java ways of reading from the
                // channel.
                BufferedReader reader = new BufferedReader(Channels.newReader(readChannel, contentType));

                readChannel.close();
            }
        }

        response.setContentType("text/html");
        response.getOutputStream().write("success".getBytes());
    }
}

完全例外:

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

1 个答案:

答案 0 :(得分:2)

您可以创建自己的multipart file upload handler,然后通过Blobstore FileService API保存文件。