在 1 个请求中将多个文件从 Android 上传到 AppEngine
Posted
技术标签:
【中文标题】在 1 个请求中将多个文件从 Android 上传到 AppEngine【英文标题】:Upload multiple files from Android to AppEngine in 1 request 【发布时间】:2012-06-12 06:27:50 【问题描述】:我了解我可以使用多部分/表单 POST 请求一次将 1 个文件上传到 AppEngine。 AppEngine 也支持uploading multiple files,但你必须做一些 hokey 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 sn-ps。
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
【问题讨论】:
一次上传多个文件需要做哪些 hokey JSP 东西? 【参考方案1】:您可以创建自己的multipart file upload handler,然后通过Blobstore FileService API 保存文件。
【讨论】:
谢谢,我认为这是正确的。将图像数据写入 blobstore 时,我的应用程序仍然崩溃。它与 java.nio.charset.IllegalCharsetNameException: image/jpeg 一起崩溃。我将 contentType 放入频道中。它不喜欢它。 NIO 在我看来真的很奇怪。 图像是字节流,但您正试图将其作为字符流读取(Reader
用于读取字符流 - 它采用字节流并将其解码为具有给定编码的字符流)。你应该使用Channels.newInputStream(..)
。以上是关于在 1 个请求中将多个文件从 Android 上传到 AppEngine的主要内容,如果未能解决你的问题,请参考以下文章
我们可以请求从 Google Cloud Storage 到 BigQuery 的多少并发上传?