文章目录
- 问题
- 使用@RequestPart注释解决方案
问题
使用swagger测试上传文件时,发现swagger没有上传文件按钮,如图所示:
@PostMapping(value = "/upload-error",consumes = "multipart/form-data") @ResponseBody public ResponseResult errorSingleFileUpload(@RequestParam(value = "file") MultipartFile myfile) throws Exception { InputStream inputStream = myfile.getInputStream(); OSSClient ossClient = ossUtil.getClientObject(); ///获取上传的文件名 String filename = myfile.getOriginalFilename(); filename = new Date().getTime() + filename; ossClient.putObject("yida-dev", filename, inputStream); // 关闭OSSClient。 ossClient.shutdown(); return ResponseResult.success(filename); }
使用@RequestPart注释解决方案@PostMapping("/upload") @ResponseBody public ResponseResult singleFileUpload(@RequestPart("file") MultipartFile myfile) throws Exception { InputStream inputStream = myfile.getInputStream(); OSSClient ossClient = ossUtil.getClientObject(); ///获取上传的文件名 String filename = myfile.getOriginalFilename(); filename = new Date().getTime() + filename; ossClient.putObject("yida-dev", filename, inputStream); // 关闭OSSClient。 ossClient.shutdown(); return ResponseResult.success(filename); }
如图:
@RequestPart 这个注释不是swagger的注释,而是spring的注释,他和他@RequestBody 注释是同一级别的。如果不标注为RequestPart,swager默认为普通json对象。