0

I have doubt for MultipartEntity. First it is deprecated or not. Second how to import MultipartEntity in my project.where to find jars. I did add jars from Apache httpclient-4.4.1,httpcore-4.4.1,httpmime-4.4.1 into my project libs folder. But i did not use multipartEntity any mistakes in my side please help me? I want to upload image from android to spring controller.

Android code is:

    HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("userMO", jsonUserMo));
        HttpPost post = new HttpPost(Constants.ROOTURL+"/media/uploadUserImage");
        post.setHeader("Content-type", "multipart/form-data; boundary=***");
        post.setEntity(new FileEntity(profileImage,"image/jpeg"));
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        contactLists.append(rd.readLine());
    } catch (Exception e) {
        e.printStackTrace();
    }

My Spring Controller :

@RequestMapping(value = { "/uploadUserImage" }, method = RequestMethod.POST)
public @ResponseBody
String uploadUserImage(@RequestParam(value = "uploadImg") MultipartFile file, @RequestParam("userMO") String userBO, HttpSession session, HttpServletRequest httpServletRequest) {
    log.info("hitting image");
    UserBO userBo = gson.fromJson(userBO, UserBO.class);
    // jboss file location to store images
    String filePath = httpServletRequest.getSession().getServletContext().getRealPath("/") + "\\resources\\userImages\\" + userBo.getRingeeUserId() + ".png";
    String fileName = file.getOriginalFilename();
    try {
        if (!file.isEmpty() && file.getBytes().length >= 5242880) {
        log.info("file size is "+file.getBytes());
        }
        if (!file.isEmpty()) {
            BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(file.getBytes()));
            BufferedImage resizedImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
            // resizedImage = originalImage.getSubimage(x1, y1, w, h);
            File destination = new File(filePath);
            // save cropped image
            ImageIO.write(resizedImage, "jpeg", destination);
        }
    } catch (Exception Exp) {
        log.info("Upload image failure");
    }
    return "";
}

I got error in android "http status 400-RequiredMultipartFile parameter 'uploadImg' is not present" How to solve this?

nmkkannan
  • 1,261
  • 4
  • 27
  • 49
  • [http://stackoverflow.com/questions/19738594/multipartentity-post-android](http://stackoverflow.com/questions/19738594/multipartentity-post-android) – M D Apr 08 '15 at 06:39
  • Not able to use MultipartEntityBuilder ? – nmkkannan Apr 08 '15 at 06:46
  • I faced same problem and found solution in below URL http://stackoverflow.com/questions/19026256/how-to-upload-multipart-form-data-and-image-to-server-in-android –  Apr 08 '15 at 06:46

1 Answers1

0
try {
    MultipartEntity entity = new MultipartEntity();
    entity.addPart("type", new StringBody("uploadImg"));
    httppost.setEntity(entity);
    HttpResponse response = httpclient.execute(httppost);
}

You should add the addPart property with your multipart entity in order to get the additional parameter which your servlet is expecting on the server end. uploadImg in your case is the additional parameter expecting by the server in your request. Hope it will resolve your problem.

Rahul Vashishta
  • 184
  • 2
  • 6
  • This "answer" lacks explanation or content as to what makes this a solution. It is difficult for a reader to solve a problem with this snippet alone. Consider adding explanation and documenting what the solution is, so your answer can be helpful and worthy of attracting upvotes. – Unihedron Apr 08 '15 at 08:14