-1

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

tereško
  • 58,060
  • 25
  • 98
  • 150
Omprakash
  • 21
  • 5

2 Answers2

0

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/

Raghav
  • 6,893
  • 3
  • 19
  • 28
  • Thanks, let me try to implement it. Will update the post after checking it. Thanks for your quick response. – Omprakash Aug 29 '13 at 11:59
  • Hi @Raghav, I tried implementing it. But I am getting "Character decoding failed" error. Don't know why it is coming. I used MultiPartFormData to save the image in the server. Client is Android. – Omprakash Sep 06 '13 at 09:37
  • You could look into it http://stackoverflow.com/questions/11856521/tomcat-getting-character-decoding-failed-in-logs-possible-malicious-attack – Raghav Sep 06 '13 at 12:13
  • `code`@RequestMapping(method = RequestMethod.POST, value = "upload", consumes=MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces=MediaType.APPLICATION_JSON_VALUE) public JSONResponse upload(@RequestBody final InputStream img0, @RequestBody final InputStream img1, @RequestBody final InputStream img2) throws Exception {`code` – Omprakash Sep 06 '13 at 13:47
  • Do i need to handle the content type at tomcat level or controller level should be OK ? I guess this error is coming when controller is trying to parse the content, means controller is reading it then it will process the data and will send the response. Why says content type not supported. – Omprakash Sep 06 '13 at 14:17
  • I checked the url you sent but no luck. I tried the changes suggested there. Not working. Any other clue ? – Omprakash Sep 06 '13 at 15:09
0
  • 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();
    }
Omprakash
  • 21
  • 5