美文网首页SpringBootspring boot
5、基于SpringBoot的文件上传

5、基于SpringBoot的文件上传

作者: youi_e050 | 来源:发表于2019-03-26 19:21 被阅读209次

三种上传方式

  • 直接上传到应用服务器
  • 上传到oss(内容存储在服务器)如 阿里云 七牛云
  • 前端将图片转成Base64编码上传(适于小容量的上传)

SpringBoot文件上传示例:——前后端不分离

创建upload模块项目


新建步骤1.png
新建步骤2.png
  • 添加web thymeleaf依赖


    新建步骤3.png
    新建步骤4.png
    新建步骤5.png

    最后新建成功

  • 配置上传属性application.properties,指定上传文件大小限制


    image.png
  • 编写Controller 通过Java.nio实现文件上传
package com.springboot.upload.controller;
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;
//上传文件控制器
//直接上传到服务器
@Controller
public class UploadController {
//指定一个临时路径作为上传目录
//private static String UPLOAD_FOLDER = "C:\Users\Liuyu\Desktop\UPLOAD\";
//遇到[http://localhost:8080](https://links.jianshu.com/go?to=http%3A%2F%2Flocalhost%3A8080),则跳转至upload.html页面
@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 {
//构建上传目标路径,找到了项目的target的classes目录
File destFile = new File(ResourceUtils.getURL("classpath:").getPath());
if(!destFile.exists()) {
destFile = new File("");
}
//输出目标文件的绝对路径
System.out.println("file path:"+destFile.getAbsolutePath());
//拼接子路径
SimpleDateFormat sf_ = new SimpleDateFormat("yyyyMMddHHmmss");
String times = sf_.format(new Date());
File upload = new File(destFile.getAbsolutePath(), "picture/"+times);
//若目标文件夹不存在,则创建
if(!upload.exists()) {
upload.mkdirs();
}
System.out.println("完整的上传路径:"+upload.getAbsolutePath()+"/"+srcFile);
//根据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);
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
// 获得文件原始名称
String fileName = srcFile.getOriginalFilename();
// 获得文件后缀名称
String suffixName = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
// 生成最新的uuid文件名称
String newFileName = uuid + "."+ suffixName;
redirectAttributes.addFlashAttribute("message", "文件上传成功"+newFileName);
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:upload_status";
}
//匹配upload_status页面
@GetMapping("upload_status")
public String uploadStatusPage() {
return "upload_status";
}
}

  • 控制器配置好thymeleaf的页面跳转及信息显示
    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>
  • 最后运行结果:


    image.png
    image.png

相关文章

  • 5、基于SpringBoot的文件上传

    三种上传方式 直接上传到应用服务器 上传到oss(内容存储在服务器)如 阿里云 七牛云 前端将图片转成Base64...

  • 5、基于SpringBoot的文件上传

    文件上传的三种方法 直接上传到应用服务器 上传到OSS(阿里云 七牛云) 前端将图片转成Base64编码上传 Sp...

  • MongoDB-4 GridFS 文件存储

    1. 配置config 2. 基于gridfs的上传,读取与下载 5. 整合SpringBoot作为文件服务展示 ...

  • 基于SpringBoot的文件上传

    上传方式 直接上传到应用服务器 上传到css(阿里云 七牛云 前端将图片转成Base64编码上传image.png

  • 基于SpringBoot的文件上传

    项目中需要上传图片可谓是经常遇到的需求,本文将介绍 3 种不同的图片上传方式。 1.云储存 常见的 七牛云,OSS...

  • 基于 SpringBoot 的文件上传

    上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等。 主要有以下几种方式 直接上传到应用服务器 上传...

  • 基于springboot的文件上传

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

  • 基于SpringBoot的文件上传

    方式: 直接上传到应用服务器 上传到OSS(阿里云、七牛云) 前端将图片转成Base64编码上传

  • 基于SpringBoot的文件上传

    文件上传方式:1.直接上传到应用服务器(速度,容量) 2.上传到oss(内容存储服务器)(阿里云,七牛云)3.前端...

  • 基于springboot的文件上传

    Spring Boot文件上传示例--前后端不分离 大致步骤总览 1.创建upload模块项目2.添加web,th...

网友评论

    本文标题:5、基于SpringBoot的文件上传

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