美文网首页
文件的上传

文件的上传

作者: 王杰磊 | 来源:发表于2019-03-26 08:59 被阅读0次

基于SpringBoot的文件上传

上传方式:

  • 1.直接上传到应用服务器
  • 2.上传到OSS(阿里云、七牛云)
  • 3.前端将图片转成Base64编译上传(仅用于小容量图片)

使用第一种方式的步骤

  • 1、创建upload模块


    image.png

    填写模块名和包名


    image.png
    注意勾选web和thymeleaf选项
    image.png
    image.png
  • 2、添加web、thymeleaf依赖
 <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>
  • 3、限制文件大小(在resources目录下的application.properties下写限制条件)
spring.servlet.multipart.max-file-size=100MB
  • 4、使用java.nio.files实现文件的上传(在com.spring.upload包下建一个controller包,controller包下建一个UploadController类,在UploadController下编写代码)
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";
    }
}
  • 5、resources->templates下建html文件
  • upload.html
<!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>
  • upload_status.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

相关文章

网友评论

      本文标题:文件的上传

      本文链接:https://www.haomeiwen.com/subject/aeswvqtx.html