我正在尝试将图像从 Android 上传到我的 rails 服务器。我所有其他数据都上传了,但我收到“错误无效的正文大小”错误。它与图像有关。下面是我的代码。帮助?!
public void post(String url) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("content_type","image/jpeg");
try {
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("picture_file_name", new StringBody("damage.jpg"));
File file = new File((imageUri.toString()));
entity.addPart("picture", new FileBody(file, "image/jpeg"));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost, localContext);
} catch (IOException e) {
e.printStackTrace();
}
}
我试过删除浏览器兼容参数,但没有帮助。我的图像被存储为一个名为 imageUri 的 URI。我正在使用回形针宝石。
谢谢!