Codejie's C++ Space

Using C++

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);
        }
    }

posted on 2015-04-13 23:53 codejie 阅读(2053) 评论(0)  编辑 收藏 引用 所属分类: 随笔而已轮子精神


只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

Using C++

导航

统计

留言簿(73)

随笔分类(513)

积分与排名

最新评论

阅读排行榜

评论排行榜