我想跳过上传URL发送到客户端的部分,从后端直接上传到blobstore。我用它来发送多部分请求,尽管我得到了:
<body><h2>HTTP ERROR 500</h2>
<p>Problem accessing/_ah/upload/ahB0NTIzNjU4OTY1ODk1Njg5ciILEhVfX0Jsb2JVcGxvYWRTZXNzaW9uX18YgICAgICAiAs M. Reason:
<pre> Missing ';'</pre></p><h3>Caused by:</h3> <pre>javax.mail.internet.ParseException: Missing ';'
at javax.mail.internet.ParameterList.<init>(ParameterList.java:135)
at javax.mail.internet.ContentType.<init>(ContentType.java:72)
at javax.mail.internet.MimeMultipart.<init>(MimeMultipart.java:98)
at com.google.apphosting.utils.servlet.MultipartMimeUtils.parseMultipartRequest(MultipartMimeUtils.java:41)
at com.google.appengine.api.blobstore.dev.UploadBlobServlet.handleUpload(UploadBlobServlet.java:173)
at com.google.appengine.api.blobstore.dev.UploadBlobServlet.access$000(UploadBlobServlet.java:71)
at com.google.appengine.api.blobstore.dev.UploadBlobServlet$1.run(UploadBlobServlet.java:117)
at java.security.AccessController.doPrivileged(Native Method)
at com.google.appengine.api.blobstore.dev.UploadBlobServlet.doPost(UploadBlobServlet.java:114)
这是发送的数据:
--__END_OF_PART__
Content-Type: image/bmp
content-transfer-encoding: binary
content-disposition: form-data; name="file"
[binary string here]
--__END_OF_PART__--
这些是标题:
accept-encoding: gzip,
content-type: multipart/form-data; boundary=__END_OF_PART__
这是代码:
BlobstoreService service = BlobstoreServiceFactory.getBlobstoreService();
String url = service.createUploadUrl("/upload");
HttpRequestFactory factory = UrlFetchTransport.getDefaultInstance().createRequestFactory();
MultipartFormContent content = new MultipartFormContent();
content.addPart(new MultipartFormContent.Part(
"file",
new InputStreamContent("image/bmp",
new ByteArrayInputStream(Base64.decodeBase64(data)))));
content.writeTo(System.out);
HttpRequest request = factory.buildPostRequest(new GenericUrl(new URL(url)), content);
request.getHeaders().setContentType(content.getMediaType().build());
request.execute();
如文档中所述,多部分主体必须是输入类型为"file"的multipart/form-data
,这将生成带有"filename"部分的Content-Disposition
标头:
Content-Disposition: form-data; name="myFile"; filename="imagename.bmp"
对于HttpClient,它应该是这样的。