基于springboot的文件上传

文件上传方式

  • 直接上传到应用服务器
  • 上传到oss(阿里云,七牛云)
  • 前端将图片转成Base64编码上传

上传服务器的一个例子

import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
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.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
/*
* 上传文件控制器
* 直接上传到服务器
* 2019.3.25
* */
@Controller
public class UploadController {
    SimpleDateFormat YYY = 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{
            //构建上传目标路径
            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(),YYY.format(new Date())+"/");
            //拼接static目录
            String fileName= srcFile.getOriginalFilename();
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            fileName= UUID.randomUUID() +suffixName;
            //若目标文件夹不存在,则创建一个
            if(!upload.exists()){
                upload.mkdirs();
            }
            System.out.println("完整的上传路径:"+upload.getAbsolutePath()+"/"+srcFile.getOriginalFilename());
            //根据srcFile的大小,准备一个字节数组
            byte[] bytes=srcFile.getBytes();
            //拼接上传路径
            /*Path path= Paths.get(UPLOAD_FOLDER+srcFile.getOriginalFilename());*/
            //通过项目路径,拼接上传路径
            Path path=Paths.get(upload.getAbsolutePath()+"/"+srcFile.getOriginalFilename());
            //最重要的一步,将源文件写入目标地址
            Files.write(path,bytes);
            //将文件上传成功的信息写入messages
            redirectAttributes.addFlashAttribute("message","文件上传成功"+srcFile.getOriginalFilename());

        }catch (IOException e){
            e.printStackTrace();
        }
        return "redirect:upload_status";
    }
    //匹配
    @GetMapping("/upload_status")
    public String uploadStatusPage(){
        return "upload_status";
    }
}
    <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>
    <title>文件上传状态显示</title>
</head>
<body>
<h2>Spring Boot的文件上传状态</h2>
<div th:if="${message}">
    <h2 th:text="${message}"/>
</div>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等。 主要有以下几种方式 直接上传到应用服务器 上传...
    yu_liu阅读 5,758评论 0 0
  • 文件上传的三种方法 直接上传到应用服务器 上传到OSS(阿里云 七牛云) 前端将图片转成Base64编码上传 Sp...
    Rebirth_914阅读 5,084评论 0 16
  • 三种上传方式 直接上传到应用服务器 上传到oss(内容存储在服务器)如 阿里云 七牛云 前端将图片转成Base64...
    youi_e050阅读 3,704评论 1 4
  • 对数组的编辑包括对数组中元素的追加、插入、删除和替换等修改操作。 1. 数组的追加 对数组元素进行添加可以使用数组...
    博为峰51Code教研组阅读 6,547评论 0 0
  • 原文片段1 R•阅读原文片段 《靠谱》 无论是发出指示的上司还是接受安排的属下,都要明确以下四个要点: 1、工作的...
    jun杰阅读 1,599评论 0 1