Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring with android image uploading

I want to upload image from android to server. My android asyc code :

    final String jsonUserMo = gson.toJson(userMO);
    final StringBuilder contactLists = new StringBuilder();
    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.setEntity(new FileEntity(new File()));
        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 controller code :

@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()) {
    //some logic 
        }
    } catch (Exception Exp) {
        log.info("Upload image failure");

    }
    return "";
}

My servlet config:

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- <property name="maxUploadSize" value="5242880" /> -->
</bean>

My problem is how to add Bitmap file in httppost to send controller. Link:Unable to add MultipartEntity because its deprecated Otherwise working for passing java object from android to controller. I want upload image file from android [using httppost] to controller. Any mistakes from me.. please help me?

like image 338
nmkkannan Avatar asked Feb 03 '26 20:02

nmkkannan


1 Answers

        final File file1 = new File(url_path);

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(http_url_path1);
        FileBody bin1 = new FileBody(file1);
        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("abc", new StringBody(abcid));
        reqEntity.addPart("xyz", new StringBody(xyzid));
        reqEntity.addPart("file", bin1);
        reqEntity.addPart("key", new StringBody(Key));
        reqEntity.addPart("authentication_token", new StringBody(Authe_Key));
        post.setEntity(reqEntity);
        HttpResponse response = client.execute(post);
        resEntity = response.getEntity();

hoping this will work...

like image 196
Arun Avatar answered Feb 06 '26 10:02

Arun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!