Question

Dear All, Working on Spring MVC. I want to upload more than one images from the client. How to achieve it. I know how to handle the multipart form data for single image. But now I am expecting some data with some images from the client.

Any help or url that will help me.

Thanks, Op

Was it helpful?

Solution 2

  • Here are the code i tried and it is working fine at my end.
//Handle multiple images
    @RequestMapping(method = RequestMethod.POST, value="upload", consumes=MediaType.MULTIPART_FORM_DATA_VALUE,
            produces=MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody JSONResponse uploadImages(HttpServletRequest req)
            throws Exception {
        try{
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req;

            Set set = multipartRequest.getFileMap().entrySet(); 
            Iterator i = set.iterator(); 
            while(i.hasNext()) { 
                Map.Entry me = (Map.Entry)i.next(); 
                String fileName = (String)me.getKey()+"_"+System.currentTimeMillis();
                MultipartFile multipartFile = (MultipartFile)me.getValue();
                System.out.println("Original fileName - " + multipartFile.getOriginalFilename());
                System.out.println("fileName - " + fileName);
                saveImage(fileName, multipartFile);
            } 
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return new JSONResponse();
    }

OTHER TIPS

Image is also a file. Whether you would be storing it in database / in file system but it is still a file.

In spring MVC, you could do as shown in the below link:

http://viralpatel.net/blogs/spring-mvc-multiple-file-upload-example/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top