Spring Boot实现文件上传功能

配置pom.xml依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

辅助类

该辅助类提供了文件上传后存储在本机的位置。

public class PathUtil {
    public static String uploadPath="E:/Jsource/FilePath/";
}

Controller

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

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

@PostMapping("/upload")
public String uploadByPost(Model model,
        @RequestParam("file") MultipartFile file){
    if (!file.isEmpty()) {
        try{
            String filePath=PathUtil.uploadPath+file.getOriginalFilename();
            BufferedOutputStream out=new BufferedOutputStream(
                    new FileOutputStream(new File(filePath)));
            out.write(file.getBytes());
            out.flush();
            out.close();
            model.addAttribute("msg", "上传文件成功");
        }
        catch(Exception e){
            e.printStackTrace();
            model.addAttribute("msg", "上传文件失败:"+e.getMessage());
        }
    }
    else{
        model.addAttribute("msg", "上传文件失败:文件为空");
    }
    return "upload";
}

@PostMapping("/uploads")
public String uploadsByPost(Model model,HttpServletRequest request){
    List<MultipartFile> files=((MultipartHttpServletRequest)request).getFiles("file");
    for(MultipartFile file:files){
        if (!file.isEmpty()) {
            try{
                String filePath=PathUtil.uploadPath+file.getOriginalFilename();
                BufferedOutputStream out=new BufferedOutputStream(
                        new FileOutputStream(new File(filePath)));
                out.write(file.getBytes());
                out.flush();
                out.close();
                model.addAttribute("msg", "上传文件成功");
            }
            catch(Exception e){
                e.printStackTrace();
                model.addAttribute("msg", "上传文件失败:"+e.getMessage());
            }
        }
        else{
            model.addAttribute("msg", "上传文件失败:文件为空");
        }
    }

    return "uploads";
}

上传页面

upload.html:单文件上传页面

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>上传单文件</title>
</head>
<span th:text="${msg}"></span>
<body>
<form method="POST" enctype="multipart/form-data" action="/upload">
<p>
    文件:<input type="file" name="file" />
</p>
<p>
    <input type="submit" value="上传" />
</p>
</form>
</body>
</html>

uploads.html:多文件上传页面

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>上传多文件</title>
</head>
<span th:text="${msg}"></span>
<body>
<form method="POST" enctype="multipart/form-data" action="/uploads">
<p>
    文件1:<input type="file" name="file" />
</p>
<p>
    文件2:<input type="file" name="file" />
</p>
<p>
    文件3:<input type="file" name="file" />
</p>
<p>
    <input type="submit" value="上传" />
</p>
</form>
</body>
</html>

限制文件上传大小

package com.example.mysite.configuration;
import javax.servlet.MultipartConfigElement;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UploadFileConfiguration {
    @Bean
    public MultipartConfigElement multipartConfigElement(){
        MultipartConfigFactory factory=new MultipartConfigFactory();
        factory.setMaxFileSize("256KB");
        factory.setMaxRequestSize("512KB");
        return factory.createMultipartConfig();
    }
}

演示

上传单文件

上传单文件演示1
上传成功
上传单文件演示2

上传多文件

上传多文件演示1
上传成功
上传多文件演示2

猜你喜欢

转载自blog.csdn.net/YINLINNEVERG/article/details/80250945