我正在尝试通过我的其余网络服务上传图片,但是当谈到多部分表单数据时,它会给我错误
HTTP Status 400 - Required MultipartFile parameter 'image' is not present
下面是我上传图片的功能
public String uploadImages(String json, File file)
{
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url + "?jsondata="+URLEncoder.encode(json));
httpPost.setHeader("content-type", "Multipart/Form-data;boundary=*****");
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("image", new FileBody(file));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost,localContext);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String sResponse = reader.readLine();
return sResponse;
} catch (Exception e)
{
Log.e(e.getClass().getName(), e.getMessage(), e);
return null;
}
}
我正在过去' image'参数通过我的multipart实体但仍然出现此错误。
答案 0 :(得分:2)
试试这个,
private String fileUpload()
{
String strRes = "";
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
mHttpClient = new DefaultHttpClient(params);
try
{
HttpPost httppost = new HttpPost(this.url);
httppost.addHeader("enctype", "multipart/form-data");
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
File filebody = new File(filepath);
multipartEntity.addPart("imagepath", new FileBody(filebody));
httppost.setEntity(multipartEntity);
HttpResponse response = mHttpClient.execute(httppost);
// mHttpClient.execute(httppost, new PhotoUploadResponseHandler());
HttpEntity r_entity = response.getEntity();
strRes = EntityUtils.toString(r_entity);
resultCode = 1;
}
catch (Exception e)
{
e.printStackTrace();
resultCode = 2;
}
return strRes;
}**