spring boot关于文件上传

导入form.html带有文件上传的表单页面

使用thymeleaf:<html xmlns:th="http://www.thymeleaf.org">,把css,js用thymeleaf替换。

在body标签中导入公共组件:

<div th:replace="~{commons/commons::leftBar}"></div>
<div class="content">
<div th:replace="~{commons/commons::topBar}"></div>

写一个controller,使得能够跳转到form页面:

@Controller
public class FormTestController {

    @GetMapping("/form")
    public String form(){
        return "form";
    }
}

在公共页面的侧边栏中添加一个能够跳转到form页面的链接:

<a th:href="@{/form}" class="dropdown-item">表单页面</a>

form.html中的表单

<form th:action="@{/upload}" method="post" enctype="multipart/form-data">
	<!--文件上传必须使用post方法,enctype为固定写法 -->
    <label for="floatingInput">邮箱</label>
    <input type="email" id="floatingInput" name="email" placeholder="name@example.com">

    <label for="floatingPassword">密码</label>
    <input type="password" id="floatingPassword" name="password" placeholder="Password">

    <label for="formFile">单文件上传</label>
    <input type="file" name="headImg" id="formFile">
	
    <label for="formFileMultiple">多文件上传</label>
    <input type="file" id="formFileMultiple" name="photos" multiple>
	<!--多文件添加属性 "multiple" -->
	
    <input type="submit" text="提交">
</form>

表单上传请求

测试表单上传是否成功

    @PostMapping("/upload")
    public String upload(@RequestParam("email")String email,
                         @RequestParam("password")String password,
                         @RequestPart("headImg")MultipartFile headImg,
                         @RequestPart("photos")MultipartFile[] photos){

        //@slf4j 测试是否上传成功

        log.info("上传的信息:email={},password={},headImg={},photos={}",
                email,password,headImg.getName(),photos.length);

        return "index";

    }

使用表单上传文件

@Controller
@Slf4j
public class FormTestController {

    @GetMapping("/form")
    public String form(){
        return "form";
    }

    @PostMapping("/upload")
    public String upload(@RequestParam("email")String email,
                         @RequestParam("password")String password,
                         @RequestPart("headImg")MultipartFile headImg,
                         @RequestPart("photos")MultipartFile[] photos) throws IOException {

        //测试是否上传成功
        log.info("上传的信息:email={},password={},headImg={},photos={}",
                email,password,headImg.getName(),photos.length);

        if(!headImg.isEmpty()){
            //保存到文件服务器,OSS服务器
            String originalFilename = headImg.getOriginalFilename();
            headImg.transferTo(new File("D:\\temp\\"+originalFilename));
        }

        if(photos.length > 0){
            for (MultipartFile photo : photos) {
                if(!photo.isEmpty()){
                    String originalFilename = photo.getOriginalFilename();
                    photo.transferTo(new File("D:\\temp\\"+originalFilename));
                }
            }
        }


        return "index";

    }
}

表单文件超过默认值(单文件1MB,一次请求总文件限制10MB)

当上传的文件超出默认值时,会报错
image

此时需要修改默认配置的上传文件的限制大小
在配置文件中添加:

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=100MB

文件上传自动配置原理

未完。

posted @ 2023-03-06 22:07  ︶ㄣ演戲ㄣ  阅读(40)  评论(0)    收藏  举报