SpringBoot-26 文件上传

1、页面表单

一定要写enctype="multipart/form-data",文件上传解析器需要根据字符串判断是否是文件上传类型操作

 <form role="form" th:action="@{/upload}" method="post" enctype="multipart/form-data">
                            <div class="form-group">
                                <label for="exampleInputEmail1">邮箱</label>
                                <input type="email" name="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">名字</label>
                                <input type="text" name="username" class="form-control" id="exampleInputPassword1" placeholder="Password">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputFile">头像</label>
                                <input type="file" name="headImg" id="exampleInputFile">
                            </div>
                            <!-- 多文件上传-->
                            <div class="form-group">
                                <label for="exampleInputFile">照片</label>
                                <input type="file" name="photoes" multiple>
                            </div>
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox"> Check me out
                                </label>
                            </div>
                            <button type="submit" class="btn btn-primary">提交</button>
                        </form>

2、表单处理controller

使用@RequestPart(“XXX”)标注文件上传

/**
 * w文件上传
 */
@Slf4j
@Controller
public class FormController {
    @GetMapping("/form_layouts")
    public String formLayout(){
        return "form/form_layouts";
    }

    /**
     * 上传文件
     *
     */
    @PostMapping("/upload")
    public String upload(@RequestParam("email") String email,
                         @RequestParam("username") String username,
                         @RequestPart("headImg") MultipartFile headImg,
                         @RequestPart("photoes") MultipartFile[] photoes
                         ) throws IOException {
        log.info("上传的信息:email=={},username=={},headImg=={},photoes=={}"+email,username,headImg.getSize(),photoes.length);
        //文件上传
        if(!headImg.isEmpty()){
            String filename = headImg.getOriginalFilename();
            headImg.transferTo(new File("D://TEST//"+filename));
        }
        if(photoes.length>0){
            for (MultipartFile photoe : photoes) {
                if(!photoe.isEmpty()){
                    String filename = photoe.getOriginalFilename();
                    photoe.transferTo(new File("D://TEST//"+filename));
                }
            }
        }

        return "main";
    }
}

3、文件上传参数配置:MultipartAutoConfiguration、MultipartProperties

yaml

  servlet:
    multipart:
      max-file-size: 10                --单个文件大小限制10M
      max-request-size: 100            --请求总大小100M

4、原理

文件上传自动配置类-MultipartAutoConfiguration-MultipartProperties

  • 自动配置好了 StandardServletMultipartResolver   【文件上传解析器】
  • 原理步骤
    • 1、请求进来使用文件上传解析器判断(isMultipart)并封装(resolveMultipart,返回MultipartHttpServletRequest)文件上传请求
    • 2、参数解析器来解析请求中的文件内容封装成MultipartFile
    • 3、将request中文件信息封装为一个Map;MultiValueMap<String, MultipartFile>

PS:FileCopyUtils类。可以实现文件流的拷贝

 @PostMapping("/upload")
    public String upload(@RequestParam("email") String email,
                         @RequestParam("username") String username,
                         @RequestPart("headerImg") MultipartFile headerImg,
                         @RequestPart("photos") MultipartFile[] photos)

 

posted @ 2021-03-27 14:11  少时也曾爱白衣  阅读(91)  评论(0)    收藏  举报