• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            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 閱讀(2227) 評論(0)  編輯 收藏 引用 所屬分類: 隨筆而已輪子精神

            公告

            Using C++

            導航

            統計

            留言簿(73)

            隨筆分類(513)

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            99久久777色| 亚洲国产成人久久综合一区77| 日韩精品无码久久久久久| 久久综合久久自在自线精品自 | 久久91精品国产91久久麻豆| 中文字幕亚洲综合久久2| 污污内射久久一区二区欧美日韩 | 青青青青久久精品国产h| 久久婷婷五月综合成人D啪| 久久精品人成免费| 久久伊人五月天论坛| 久久A级毛片免费观看| 久久久久久亚洲精品不卡| 色综合久久久久久久久五月| 久久无码av三级| 人人狠狠综合久久88成人| 久久精品国产色蜜蜜麻豆| 99久久精品国产麻豆| 久久国语露脸国产精品电影| 久久99精品久久久久久齐齐| 狠狠色丁香久久婷婷综合五月 | 亚洲&#228;v永久无码精品天堂久久 | 久久精品麻豆日日躁夜夜躁| 亚洲精品99久久久久中文字幕 | 香蕉久久夜色精品国产2020 | 国产精品久久永久免费| 最新久久免费视频| 色99久久久久高潮综合影院| 久久精品国产欧美日韩| 久久最近最新中文字幕大全| 久久婷婷五月综合色高清| 国产精品久久久久久久app| 欧美激情精品久久久久久| 久久久久久国产精品美女| 国产精品久久久久久久午夜片 | 久久黄色视频| 久久人人爽人人精品视频| 久久综合久久伊人| 中文字幕无码av激情不卡久久| 久久久亚洲精品蜜桃臀| 亚洲欧美日韩久久精品|