使用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;
  }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM