文件的上传

2019-03-26  本文已影响0人  王杰磊

基于SpringBoot的文件上传

上传方式:

使用第一种方式的步骤

 <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>
spring.servlet.multipart.max-file-size=100MB
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 上传文件控制器
 * 直接上传到服务器
 */
@Controller
public class UploadController {
    //制定一个临时路径
    //private  static String UPLOAD_FOLDER="E:/temp/";

    //遇到http://localhost:8080则跳转到upload.html
    @GetMapping("/")
    public String index(){
        return "upload";
    }
    @PostMapping("/upload")
    public String fileUpload(@RequestParam("file") MultipartFile srcFile, RedirectAttributes redirectAttributes){
        if(srcFile.isEmpty()){
            redirectAttributes.addFlashAttribute("message","选择一个文件");
            return "redirect:upload_status";
        }
        try {
            File destFile=new File(ResourceUtils.getURL("classpath").getPath());
            if(destFile.exists()){
                destFile=new File("");
            }
            //输出目标文件的绝对路径
            System.out.println("file path:"+destFile.getAbsolutePath());
            //拼接子路经
            File upload=new File(destFile.getAbsolutePath(),"static/");
            //目标文件不存在,创建一个
            if(!upload.exists()){
                upload.mkdirs();
            }
            System.out.println("完整的上传路径:"+upload.getAbsolutePath()+"/"+srcFile.getOriginalFilename());
            byte[] bytes=srcFile.getBytes();
            //拼接上传路径
            //Path path= Paths.get(UPLOAD_FOLDER+srcFile.getOriginalFilename());
            //通过项目路径,拼接上传路径
            Path path=Paths.get(upload.getAbsolutePath()+"/"+srcFile.getOriginalFilename());
            Files.write(path,bytes);
            redirectAttributes.addFlashAttribute("message","文件上传成功");
        }catch (IOException e){
            e.printStackTrace();
        }
        return "redirect:upload_status";
    }
    //匹配upload_status
    @GetMapping("/upload_status")
    public String uploadStatusPage(){
        return "upload_status";
    }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>spring boot文件上传</title>
</head>
<body>
<form method="post" action="/upload" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" value="上传">
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>文件上传状态显示</title>
</head>
<body>
<h2>spring boot文件上传状态</h2>
<div th:if="${message}">
    <h2 th:text="${message}"></h2>
</div>
</body>
</html>

运行结果


image.png

选择文件


image.png
上传
image.png

查看文件夹(上传成功)


image.png
上一篇下一篇

猜你喜欢

热点阅读