【发布时间】:2014-06-30 17:07:01
【问题描述】:
我正在尝试将图像从 Android 客户端上传到我的 Google App Engine Blobstore,但遇到了问题。我发现的所有相关帖子都使用现已弃用的 HttpEntity。我的代码导致我的服务器返回 500 错误。
Android 客户端代码:
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("file", new FileBody(file, ContentType.MULTIPART_FORM_DATA));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(reqEntity.build());
HttpResponse httpResponse = httpclient.execute(httppost);
String response = EntityUtils.toString(httpResponse.getEntity());
服务器(App Engine)代码:
public class BlobUpload extends HttpServlet {
@Override
private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(req);
BlobKey blobKey = blobs.get("file").get(0);
res.setStatus(HttpServletResponse.SC_OK);
res.setContentType("text/plain");
if (blobKey == null) {
res.getWriter().print("failure");
} else {
res.getWriter().print("success");
}
}
}
根据我看到的所有示例,我尝试了其他几种排列,但均无济于事。这包括通过将这些行添加到我的客户端代码来将 Content-Type 设置为 Multipart/Form-Data:
builder.setBoundary("--theboundary--");
httppost.addHeader("Content-Type", "multipart/form-data; boundary=--theboundary--");
我已经测试了我的 servlet 以确保它可以正常工作,所以我确信问题在于我将数据发布到服务器的方式。我已经搜索了 MIME 和 App Engine 文档(我发现这两个文档都很有限)并且找不到解决方案。任何想法/建议或工作示例将不胜感激。
【问题讨论】:
标签: java android google-app-engine http-post