使用PostMan测试文件上传接口

1.header,由于没有登录不能访问,要传一个token

2.body,见图

@ApiOperation(value = "上传身份图片返回上传后文件名")
  @PostMapping(value = "/uploadImgFile", headers = "content-type=multipart/form-data")
  public ErrorObject uploadImgFile(@RequestParam("file") List<MultipartFile> files, HttpServletResponse response)
      throws Exception {
    ErrorObject error = new ErrorObject();
    for (MultipartFile file : files) {
      Map<String, Object> map = new HashMap<>();
      String ext = StringUtils.substringAfterLast(file.getOriginalFilename(), ".");
      if ("exe".equalsIgnoreCase(ext) || "bat".equalsIgnoreCase(ext)) {
        error.setMsg("禁止上传的文件格式");
        error.setSuccess(false);
        return error;
      }
      try {
        File folder = new File(saveFilePath + "\\");
        if (!folder.exists()) {
          folder.mkdirs();
        }
        String fileName = UUID.randomUUID() + "." + StringUtils.substringAfterLast(file.getOriginalFilename(), ".");
        String filePath = saveFilePath + "\\" + fileName;
        log.debug(filePath);
        FileCopyUtils.copy(file.getBytes(), new File(filePath));
        String[] arrayStr = fileName.split("\\.");
        map.put("url", "/upload/viewImg/" + arrayStr[1] + "/" + arrayStr[0]);
        map.put("fileName", fileName);
        error.setSuccess(true);
        error.setMsg("上传成功");
        error.setMap(map);
      } catch (Exception e) {
        log.error("{}", e);
        error.setMsg("上传失败!" + e.getMessage());
        error.setSuccess(false);
        return error;
      }
    }
    return error;
  }

猜你喜欢

转载自blog.csdn.net/xiao297328/article/details/83026700