我想将图片上传到服务器。
这是代码,
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(Constants.yigit);
Charset chars = Charset.forName("UTF-8"); // Setting up the encoding
MultipartEntity reqEntity = new MultipartEntity();
StringBody jsonBody = new StringBody(getNewDemandRequestParams(), "application/json",null);
FormBodyPart jsonBodyPart = new FormBodyPart("data", jsonBody);
reqEntity.addPart(jsonBodyPart);
if (getMainActivity().getImagesSavedData(0).size() > 0) {
for (int i = 0; i < getMainActivity().getImagesSavedData(0).size(); i++) {
File _file = new File(getMainActivity().getImagesSavedData(0).get(i).getFilePath());
FileBody _fileBody = new FileBody(_file, "image/jpg", "UTF-8");
FormBodyPart fileBodyPart = new FormBodyPart(getMainActivity().getImagesSavedData(0).get(i).getImageName().replace(".jpg", ""), _fileBody);
reqEntity.addPart(fileBodyPart);
reqEntity.addPart(getMainActivity().getImagesSavedData(0).get(i).getImageName().replace(".jpg",""), _fileBody);
}
}
post.setEntity(reqEntity);
String result = EntityUtils.toString(reqEntity);
Log.e("rsul", result);
HttpResponse response = client.execute(post);
resEntity = response.getEntity();
final String response_str = EntityUtils.toString(resEntity);
}
但问题是jsonBodyPart包含斜杠。
请求这样的身体:
{&#34;数据&#34; =&GT;&#34; {\&#34;操作\&#34;:\&#34; YENITALEP \&#34; \&#34;应用\&#34;:{\&#34;版本\&#34;:\&#34; verisyon \&#34;},\&#34;数据\&#34;:{\&#34;发票\&#34;:[{\&#34; imageName \&#34;:\&#34; 1395914025134 \&#34; \&#34;注意\&#34;:\&#34; \&#34; \&#34;类型\&#34;:\&#34; FATURA \&#34; \&#34; typeNo \&#34;:\&#34; 0 \& #34;}],\&#34;注意\&#34;:\&#34; \&#34; \&#34;通知\&#34;:[{\&#34;类型\ &#34;:\&#34; BeniArayñ\&#34; \&#34; typeNo \&#34;?:\&#34; 0 \&#34;}]},\&#34 ;设备\&#34;:{\&#34; hardwareModel \&#34;:\&#34; M7 \&#34; \&#34;模型\&#34;:\&#34; HTC 一个\&#34; \&#34; systemVersion \&#34;:\&#34; 4.4.2 \&#34; \&#34; UID \&#34;:\&#34; 00000000-7f39-faab-b500-7f280e9b4fed \&#34;},\&#34;时间戳\&#34;:\&#34;日期(1391073711000 + 0200)\&#34;}&#34 ;, &#34; 1395914025134&#34; = GT;#, @original_filename =&#34; 1395914025134.jpg&#34;,@ content_type =&#34; image / jpg; charset = UTF-8&#34;,@ headers =&#34; Content-Disposition:form-data; 命名= \&#34; 1395914025134 \&#34 ;; filename = \&#34; 1395914025134.jpg \&#34; \ r \ nConContent-Type:image / jpg; charset = UTF-8 \ r \ nContent-Transfer-Encoding:binary \ r \ n&#34;&gt;}
如何使用multipart发布复杂的json对象和图像?谢谢你的帮助
答案 0 :(得分:0)
检查此代码后,使用此代码将图像上传到服务器
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(urls[0]);
MultipartEntity multipartContent = new MultipartEntity();
for(int i=0;i<allimagespath.size();i++){
Bitmap bm = ShrinkBitmap(allimagespath.get(i), 140, 140);
String format = allimagespath.get(i).substring((allimagespath.get(i).lastIndexOf(".")+1) , allimagespath.get(i).length());
Bitmap bit=Bitmap.createScaledBitmap(bm, 140, 140, true);
ByteArrayOutputStream blob = new ByteArrayOutputStream();
if(format.equalsIgnoreCase("png")){
bit.compress(CompressFormat.PNG, 100 , blob);
}else{
bit.compress(CompressFormat.JPEG, 100 , blob);
}
bitmapdata = blob.toByteArray();
ByteArrayBody thumbbmp = new ByteArrayBody(bitmapdata, "thumb."+format);
FileBody bin2 = new FileBody(new File(allimagespath.get(i)));
multipartContent.addPart("uploaded_file["+i+"]", bin2);
multipartContent.addPart("uploaded_thumb["+i+"]", thumbbmp);
}
multipartContent.addPart("count", new StringBody(""+allimagespath.size()));
postRequest.setEntity(multipartContent);
HttpResponse response = httpClient.execute(postRequest);
HttpEntity entity = response.getEntity();
is = entity.getContent();