我知道我可以使用 multipart/form POST 请求一次将 1 个文件上传到 AppEngine。AppEngine 还支持上传多个文件,但您必须做一些花哨的 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