美文网首页全栈开发
spring boot文件上传

spring boot文件上传

作者: MichaelDing | 来源:发表于2019-08-29 16:03 被阅读0次

如图所示建立spring boot工程,目录结构如下:

6D34E54A45CF26699FD9A6E44A899C51.png
  1. 在controller包下建立UploadController,控制器代码如下:
package com.ssm.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
@Controller
public class UploadController {
    SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
    @GetMapping("/")
    public String index(){
        return  "upload";
    }
    @PostMapping("/upload")
    public String fileUpload(@RequestParam("file")MultipartFile srcFile, RedirectAttributes redirectAttributes){
        //前端没有选择文件,srcFile为空
        if (srcFile.isEmpty()){
            redirectAttributes.addFlashAttribute("message","请选择一个文件");
            return "redirect:upload_status";
        }
        //选择了文件,开始进行上传操作
        try {
            String coverPath_pre = "/usr/local/static";
            String coverPath = coverPath_pre +"/upload/" + "springboot" + "/";

            File destFile=new File(coverPath);
            System.out.println("file path"+destFile.getAbsolutePath());
            String fileName= srcFile.getOriginalFilename();
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            fileName= UUID.randomUUID() +suffixName;
            File upload=new File(destFile.getAbsolutePath(),dateformat.format(new Date())+"/" + fileName);
            //若目标文件夹不存在,则创建一个
            if(!upload.exists()){
                upload.mkdirs();
                System.out.print(upload.exists());
            }
            System.out.println("完整的上传路径:"+upload.getAbsolutePath()+"/");
            srcFile.transferTo(upload);
            redirectAttributes.addFlashAttribute("message","文件上传成功!,文件已重命名为:"+fileName);
        }catch (IOException e){
            e.printStackTrace();
        }
        return "redirect:upload_status";
    }
    //匹配upload_status页面
    @GetMapping("/upload_status")
    public String uploadStatusPage(){
        return "upload_status";
    }
}
  1. 在resource 的templates目录下新建upload.html 和upload_status.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}"/>
</div>
</body>
</html>
  1. 项目运行成功后浏览器输入:localhost:8089


    图片.png
    2C834102F457C735412A43C0DD7A7476.png

    4 .通过Nginx浏览器即可访问上传的图片:


    http://118.31.3.252/upload/springboot/2019-08-29/8c718b83-eb80-4b02-90bc-3243be1412f9.jpg
    至此图片上传完成,是不是很简单!喜欢就点个赞吧!

相关文章

网友评论

    本文标题:spring boot文件上传

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