我試圖從Android客戶機上傳圖像到我的Google App Engine Blobstore,並遇到問題。我發現的所有相關帖子都使用了現在已棄用的HttpEntity。我的代碼導致我的服務器返回一個500錯誤。將圖像上傳到Google App Engine Blobstore - Java
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());
服務器(應用程序引擎)代碼:
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");
}
}
}
我已經嘗試我所看到的一切都是徒勞基於實例其他幾個permuatations。這包括通過添加這些行到我的客戶端代碼設置內容類型爲multipart/form-數據:
builder.setBoundary("--theboundary--");
httppost.addHeader("Content-Type", "multipart/form-data; boundary=--theboundary--");
我測試了我的servlet,以確保它的工作原理,所以我敢肯定問題出在路上我將我的數據發佈到我的服務器。我搜索了MIME和App Engine文檔(我發現這兩個文檔都很有限),並找不到解決方案。任何想法/建議或工作示例將不勝感激。