springmvc_文件上传

1.页面表单

<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="headerImg" id="exampleInputFile">
    </div>
    <div class="form-group">
        <label for="exampleInputFile">生活照</label>
        <input type="file" name="photos" multiple><!--提交多个文件-->
    </div>
    <div class="checkbox">
        <label>
            <input type="checkbox"> Check me out
        </label>
    </div>
    <button type="submit" class="btn btn-primary">提交</button>
</form>    

2.文件上传代码

/**
     * MultipartFile 自动封装上传过来的文件
     * @param email
     * @param username
     * @param headerImg
     * @param photos
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public String upload(@RequestParam("email") String email,
                         @RequestParam("username") String username,
                         @RequestPart("headerImg") MultipartFile headerImg,
                         @RequestPart("photos") MultipartFile[] photos) throws IOException {
        log.info("上传的信息:email={},username={},headerImg={},photos={}", email, username, headerImg.getSize(), photos.length);
        if (!headerImg.isEmpty()) {
            //生成保存后的文件名,UUID 32位随机字母+数字,把 - 替换为 ""
            String randomId = UUID.randomUUID().toString().replaceAll("-", "");
            //获取上传的文件名
            String originalFilename = headerImg.getOriginalFilename();
            //根据最后一个 . 截取,获取源文件的后缀名/扩展名 .jsp
            String exName = originalFilename.substring(originalFilename.lastIndexOf("."));
            //拼接上传后的文件名并上传
            String newName = randomId + exName;
            headerImg.transferTo(new File("E:\\SSM\\" + newName));
        }
        if (photos.length > 0) {
            for (MultipartFile photo : photos) {
                if (!photo.isEmpty()) {
                    String randomId = UUID.randomUUID().toString().replaceAll("-", "");
                    String originalFilename = photo.getOriginalFilename();
                    String exName = originalFilename.substring(originalFilename.lastIndexOf("."));
                    String newName = randomId + exName;
                    photo.transferTo(new File("E:\\SSM\\" + newName));
                }
            }
        }
        return "main";
    }

3.自动配置原理

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

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

FileCopyUtils。实现文件流的拷贝

 

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

 

 

4.设置文件大小

在 application.properties 中设置

 

 

 

posted @ 2021-09-25 23:02  蔡地像徐坤  阅读(50)  评论(0编辑  收藏  举报