使用postman上传文件同时传JSON数据到数据库

  1. Headers 中类型为空
    使用postman上传文件同时传JSON数据到数据库_第1张图片
    2.填写测试数据
    使用postman上传文件同时传JSON数据到数据库_第2张图片
    上传文件时注意选择文件File
    使用postman上传文件同时传JSON数据到数据库_第3张图片
  2. Controller 层相关代码
 @PostMapping(value = "/save")
    public void save(@RequestParam(value = "files") MultipartFile upfiles[], GoodsPicture goodsPicture) throws IOException {
        try{
            if(upfiles!=null) {
                for (int i=0;i<upfiles.length;i++) {
                    if (!upfiles[i].isEmpty()) {
                        String fileName = upfiles[i].getOriginalFilename();
                        File file = new File(url + fileName);
                        // 将MulitpartFile文件转化为file文件格式
                        upfiles[i].transferTo(file);
                        Map response = fileService.uploadFile(file);
                        Object imageName = response.get("imgName");
                        // 图片地址
                        String picture_url = path + "/" + imageName;
                        goodsPicture.setGpAddress(picture_url);
                        goodsPictureService.save(goodsPicture);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

你可能感兴趣的:(java)