美文网首页
postman上传文件

postman上传文件

作者: 墨色尘埃 | 来源:发表于2018-01-07 12:44 被阅读4250次

    postman上传文件调接口,使用POST的form-data方式
    文件数据在后台方法中的HttpServletRequest中。
    ①request中文件信息
    ②其实request中不仅有文件信息,还有其他文本信息也在里面,只不过需要通过key获取到该json字符串然后转成对象,这样可能会比较麻烦,需要前端Content-Type为form-data格式。
    通过postman模拟
    ②-1 设置为text格式,将json串粘贴进去
    ②-2 使用文本的话,需要将request强转成MultipartHttpServletRequest再读文件里的信息

    ①上传文件.png
    ②后台使用map接收文本和文件,content-type为form-data.png
    ②request接收content里的内容,postman这样模拟。如果form-data上传的是文件,需要把request强转成MuiltPara..,再从里面读流.png

    control类中方法

     /**
         * 导入
         */
        @RequestMapping(value = "/leadingIn", method = RequestMethod.POST)
        public ResponseObj<Boolean> leadingIn(InvestDetail investDetail,
                                              HttpServletRequest request,
                                              Map<String, InputStream> files) {
            //测试
            try {
                MultipartHttpServletRequest mulRequest = (MultipartHttpServletRequest) request;
                Set<Map.Entry<String, MultipartFile>> set = mulRequest.getFileMap().entrySet();
                Map<String, InputStream> listFile = new LinkedHashMap<>();
                System.out.println("个数" + set.size());
                for (Map.Entry<String, MultipartFile> each : set) {
                    String fileName = each.getKey();
                    MultipartFile file = each.getValue();
                    //这里需要上传FTP
                    try {
                        listFile.put(fileName, file.getInputStream());
                    } catch (Exception ex) {
                        return new ResponseObj<>(false, null);
                    }
                }
    
                String formjson = mulRequest.getParameter("content");
                ObjectMapper mapper = new ObjectMapper();
                mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
                mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                
                OrderReturn orderReturn = mapper.readValue(formjson, OrderReturn.class);
                boolean result = iInstallWorkOrder.receiptOrder(orderReturn, listFile);
    
    //            boolean result = iInstallWorkOrder.upLoadFile(listFile);
    //            boolean result = true;
    
                return new ResponseObj<>(result, null);
            } catch (Exception ex) {
                return new ResponseObj<>(false, null);
            }
    
        }
    
    public ResponseObj<Boolean> saveOrSubmit(HttpServletRequest request) {
            Business business = null;
    
            try {
                String json = request.getParameter("content");
                if (!StringUtils.isEmpty(json)) {
                    ObjectMapper oMapper = new ObjectMapper();
                    oMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                    business = oMapper.readValue(json, Business.class);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
    

    相关文章

      网友评论

          本文标题:postman上传文件

          本文链接:https://www.haomeiwen.com/subject/grppnxtx.html