SPRING : Handle JSON and Image object in One Request

SPRING : Handle JSON and Image object in One Request


    代码这里~

    @RequestMapping(value = "/req", method = RequestMethod.POST)
    @ResponseBody
     public ResponseEntity<Post.Response> post(HttpServletRequest request) {

        Post.Response resp =  null;
        
        Post.Request req =  null;
        String file =  null;
        
        CommonsMultipartResolver multipartResolver =  new CommonsMultipartResolver(request.getSession().getServletContext());  

         if (multipartResolver.isMultipart(request)) {  
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;  
  
             try {
                Collection<Part> parts = multiRequest.getParts();
                
                 boolean hasJSON =  false;
                 boolean hasFile =  false;
                
                 for ( final Part p : parts) {
                    
                     if (hasJSON) {
                         continue;
                    }

                     if (p.getContentType().contains("application/json")) {

                        ObjectMapper mapper =  new ObjectMapper();
                        req = mapper.readValue(p.getInputStream(), Post.Request. class);
                        
                         if (req !=  null) {
                            resp =  new Post.Response();
                            resp.setX(100);
                            
                            hasJSON =  true;
                        }

                    }  else  if (p.getContentType().contains("image/jpeg")) {
                        ++ index;
                        file = UPLOAD_ROOT + String.valueOf(index) + ".jpg";
                        
                        InputStream is = p.getInputStream();
                        
                        File localFile =  new File(file);                        
                        OutputStream os =  new FileOutputStream(localFile);
                         int bytesRead = 0;
                         byte[] buffer =  new  byte[8192];
                         while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
                            os.write(buffer, 0, bytesRead);
                        }
                        os.close();
                        
                        hasFile =  true;
                    }
                    
                     if (hasJSON && hasFile) {
                         break;
                    }
                }
            }  catch (IOException e1) {
                 //  TODO Auto-generated catch block
                e1.printStackTrace();
            }  catch (ServletException e1) {
                 //  TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
         if (req !=  null && file !=  null) {
            resp = calc.calc(req, file);
        }
        
         if (resp !=  null) {
             return  new ResponseEntity<Post.Response>(resp, HttpStatus.OK);
        }  else {
             return  new ResponseEntity<Post.Response>(resp, HttpStatus.BAD_REQUEST);
        }
    }

你可能感兴趣的:(SPRING : Handle JSON and Image object in One Request)