SpringBoot-文件上传(单文件以及多文件上传)

SpringBoot-文件上传(单文件以及多文件上传)

在springboot中进行单文件以及多文件的上传
HTML代码

  <form role="form" th:action="@{/upload}" method="post" enctype="multipart/form-data">
                           
                            <div class="form-group">
                                <label for="exampleInputFile">用户头像</label>
                                <input type="file" name="headerImg" id="exampleInputFile">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputFile">生活照</label>
                                <input type="file" name="photos" multiple>
                            </div>
                           
                            <button type="submit" class="btn btn-primary">提交</button>
                        </form>

Controller代码

 /***
     * MultipartFile自动封装上传过来的文件
     * @param email
     * @param username
     * @param headerImg
     * @param photos
     * @return
     */
    @PostMapping("/upload")
    public String upload(
                         @RequestParam("headerImg") MultipartFile headerImg,
                         @RequestParam("photos") MultipartFile[] photos) throws IOException {
      
        if(!headerImg.isEmpty()){
            //保存到文件服务器 oss服务器
            String originalFilename = headerImg.getOriginalFilename();
            headerImg.transferTo(new File("E:\\Image\\"+originalFilename));
        }
        if (photos.length>0){
            for (MultipartFile photo : photos) {
                if (!photo.isEmpty()){
                    String originalFilename = photo.getOriginalFilename();
                    photo.transferTo(new File("E:\\Image\\"+originalFilename));
                }
            }
        }
        return "main";

    }

上传示例图
在这里插入图片描述
在这里插入图片描述
Tips:
springboot的默认配置是单个上传不超过1MB,总体上传不超过10MB,如果选择文件过大则会抛异常
在这里插入图片描述
解决方案:在application.properties中进行自定义配置

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=100MB
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值