解决springBoot+swagger 实现新建时文件上传

swagger单文件上传

@Value("${web.upload-path}")
private String webUploadPath;//这个实在配置文件配置的

@PostMapping(value = "/upload", consumes = "multipart/*", headers = "content-type=multipart/form-data")
@ApiOperation(value = "上传图片", notes = "上传图片", httpMethod = "POST")
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "上传成功!"),
        @ApiResponse(code = 500, message = "上传失败!")
})
public String upload(@ApiParam(value = "医院图片", required = true) MultipartFile file) {
    if (!file.isEmpty()) {
        if (file.getContentType().contains("image")) {
            try {
                String temp = "images" + File.separator + "upload" + File.separator;
                // 获取图片的文件名
                String fileName = file.getOriginalFilename();
                // 获取图片的扩展名
                String extensionName = fileName.substring(fileName.indexOf("."));
                // 新的图片文件名 = 获取时间戳+"."图片扩展名
                String newFileName = String.valueOf(System.currentTimeMillis()) + "." + extensionName;
                // 数据库保存的目录
                String datdDirectory = temp.concat(String.valueOf(1)).concat(File.separator);
                // 文件路径
                String filePath = webUploadPath.concat(datdDirectory);

                File dest = new File(filePath, newFileName);
                if (!dest.getParentFile().exists()) {
                    dest.getParentFile().mkdirs();
                }
                // 上传到指定目录
                file.transferTo(dest);
                return "上传成功";
            }catch (Exception e){
                return "上传失败";
            }
        }
    }
    return "上传成功";
}

swagger展示如下图
在这里插入图片描述

多个文件和普通表单数据一起传递到后端只能通过postman测试,swagger好像不能多个文件上传
上传代码

@PostMapping(value = “/uploadFile”, consumes = “multipart/*”, headers = {“content-type=multipart/form-data”,“content-type=application/json”})
@ApiOperation(value = “上传图片”, notes = “上传图片”, httpMethod = “POST”,response = User.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = “上传成功!”),
@ApiResponse(code = 500, message = “上传失败!”)
})
public String uploadFile(@ApiParam(value = "医院图片", required = true) MultipartFile[] files,User user) {
    System.out.println(user);
    for(MultipartFile file : files) {
        if (!file.isEmpty()) {
            if (file.getContentType().contains("image")) {
                try {
                    String temp = "images" + File.separator + "upload" + File.separator;
                    // 获取图片的文件名
                    String fileName = file.getOriginalFilename();
                    // 获取图片的扩展名
                    String extensionName = fileName.substring(fileName.indexOf("."));
                    // 新的图片文件名 = 获取时间戳+"."图片扩展名
                    String newFileName = String.valueOf(System.currentTimeMillis()) + "." + extensionName;
                    // 数据库保存的目录
                    String datdDirectory = temp.concat(String.valueOf(1)).concat(File.separator);
                    // 文件路径
                    String filePath = webUploadPath.concat(datdDirectory);

                    File dest = new File(filePath, newFileName);
                    if (!dest.getParentFile().exists()) {
                        dest.getParentFile().mkdirs();
                    }
                    // 上传到指定目录
                    file.transferTo(dest);
                } catch (Exception e) {
                    return "上传失败";
                }
            }
        }
    }
    return "上传成功";

}

上传文件,普通表单数据传递到后端报这个错

{
“timestamp”: 1566885167423,
“status”: 415,
“error”: “Unsupported Media Type”,
“exception”: “org.springframework.web.HttpMediaTypeNotSupportedException”,
“message”: “Content type ‘multipart/form-data;boundary=--------------------------194943065565078399034201;charset=UTF-8’ not supported”,
“path”: “/uploadFile”
}

把接受参数前面的@RequestBody注解去掉就可以了,
普通表单的参数只可以通过属性传递,不能通过json字符串,我是用postman测试的,但是没有用vue前端传递参数,但是大致是一样的
附上postman的测试案例
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值