SpringBoot simple file upload function

Static page writing

The encoding type is set to multipart / form-data to support text and file upload, and the input type is file

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件上传</title>
</head>
<body>
    <form action="/fileUploadController" method="post" enctype="multipart/form-data">
        <input type="file" name="file" value="上传">
        <input type="submit">
    </form>
</body>
</html>

Controller

@RestController
public class FileUploadController {
    @PostMapping("/fileUploadController")
    public String fileUpload(MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("f:/"+file.getOriginalFilename()));//以文件原本名字存储在F盘
        return "OK";
    }
}

Set upload file size and total upload

Configure file upload restrictions in application.properties

#配置单个上传文件的大小限制
spring.servlet.multipart.max-file-size=2MB
#配置在一次请求中上传文件的总容量的限制
spring.servlet.multipart.max-request-size=20MB
Published 28 original articles · praised 0 · visits 722

Guess you like

Origin blog.csdn.net/William_GJIN/article/details/105391358