我正在尝试使用此代码在服务器上上传图像和文本,但是使用此代码我遇到了一些错误“内容长度标题已经存在”或者如果我没有设置内容lenget那么它返回“> 411长度“
请给我一些解决方案
这是我的代码
String url = webserviceURLs.createQuestion+"?token"+OkayUserProfileProps.tocken;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setHeader("Content-Type", "multipart/form-data");
httppost.addHeader("Accept", "application/json");
byte[] abc = convertImageToByte(Uri.parse(imageName.get(0)));
InputStream in = new ByteArrayInputStream(abc);
ContentBody mimePart = new InputStreamBody(in, "file1");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("userquestion[question_id]", new StringBody("2"));
reqEntity.addPart("userquestion[looks][0][photo]", mimePart);
httppost.setEntity(reqEntity);
int a=abc.length;
httppost.setHeader(HTTP.CONTENT_LEN,""+abc.length);
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String retSrc = EntityUtils.toString(resEntity);
System.out.println("Responce "+retSrc);
}
return null;
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e("Exception", e.getMessage() + "");
return null;
}
答案 0 :(得分:0)
试试这个,
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("image", new FileBody(new File("your image path"));