I am trying to upload image through my rest webservice , but when it comes to multipart-form data it gives me error
HTTP Status 400 - Required MultipartFile parameter 'image' is not present
below is my function to upload image
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;
}
}
I am passing 'image' parameter through my multipart entity but still getting this error.
try this,
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;
}**