使用PostMan测试文件上传接口

news/2024/7/5 1:46:53

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


http://lihuaxi.xjx100.cn/news/244676.html

相关文章

极限编程 (Extreme Programming) - 迭代计划 (Iterative Planning)

(Source: XP - Iteration Planning) 在每次迭代开始时调用迭代计划会议&#xff0c;以生成该迭代的编程任务计划。每次迭代为1到3周。 客户从发布计划中按照对客户最有价值的顺序选择用户故事进行此次迭代。还选择了要修复的失败验收测试。客户选择的用户故事的估计总计达到上次…

Jvm面试题总结及答案 300道(针对Jvm的面试题 )

Jvm面试题及答案&#xff08;Jvm面试题大全带答案&#xff09;发现网上很多Jvm面试题及答案整理都没有答案&#xff0c;所以花了很长时间搜集&#xff0c;本套Jvm面试题大全&#xff0c;有大量经典的Jvm面试题以及答案&#xff0c;包含Jvm语言常见面试题、面试经验技巧等&#…

【实用总结】DOM节点className操作

经常用到的小工具函数&#xff0c;每次用到都重写一遍&#xff0c;有点浪费体力&#xff0c;于是决定分阶段整理下&#xff0c;需要用到的时候就直接拷过来用了&#xff5e; 对于dom节点的className&#xff0c;常用操作不外乎增、删、查&#xff08;改可以用增删事先&#xff…

Postman系列之@RequestBody修饰的对象传参数

请求地址&#xff1a;http://localhost:8283/api/tblContractPre/updateContractInfoToErp 请求参数&#xff1a; { "city":"深圳市", "uid":"张三", "deedsn":"PRE-HA180900061", "rooms…

Matlab之print,fprint,fscanf,disp函数

print&#xff1a; print函数可以把函数图形保存成图片&#xff1a; [plain] view plaincopy minbnd -4*pi; maxbnd 4*pi; t minbnd:0.1*pi:maxbnd; plot(t, sin(t), g, Linewidth, 2); line([minbnd, maxbnd], [0, 0]); %绘制x轴 axis([-10, 10, -2, 2]) %定义显示的坐标区…

一个free异常引发的异常

有同事反馈说自己的线程不工作&#xff0c;查看堆栈发现其打印如下&#xff1a; #0 0x00007f291f72e7be in __lll_lock_wait_private () from /lib64/libc.so.6 #1 0x00007f291f6c2e4e in _L_lock_9925 () from /lib64/libc.so.6 #2 0x00007f291f6c1101 in free () from /li…

房价与阶级

最近一线城市房价似乎大幅度飙升&#xff0c;有人说这是崩溃前夕的疯狂。今晨在微博上看到&#xff0c;王健林说未来20年一线城市的房价会持续上涨&#xff0c;评论区里被谩骂淹没。关于大城市房价&#xff0c;我没有能力做任何分析和预测。只对崩溃论发表一些想法。前几天逛汽…