基于SpringBoot的文件上传

文件上传方式:1.直接上传到应用服务器(速度,容量) 2.上传到oss(内容存储服务器)(阿里云,七牛云)3.前端将图片转出Base64编码上传(小容量的上传)

第一种:先键一个新的模块upload


5.png
1.png
2.png
3.png

6.png
  • 然后finish,建立包和三个类


    7.png

UploadController类

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,则跳转至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";
}
}

upload.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SpringBoot文件上传页面</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>SpringBoot的文件上传状态</h2>
<div th:if="{message}"> <h2 th:text="{message}"/>
</div>
</body>
</html>

  • 运行结果


    8.png

    9.png

    打包文件上传项目为jar类型的步骤:
    1.双击以下install


    10.png

    成功的图片
    13.png

    生成一个jar包
    14.png
  • 停到服务器

  • 把这个jar包复制粘贴到文件夹中


    16.png
  • 把cmd命令行打开(打开到jar包的位置)

java -java upload.jar

17.png

在Google里面打开http://localhost:8080/upload_status
20.png

能上传说明已经成功了
上传图片的路径:F:\SpringBootStudy\spring-boot-damo\upload\target\classes

1.文件名在服务器端可以重命名(扩展名不变)

2.上传目录自动创建为当前系统日期
21.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等。 主要有以下几种方式 直接上传到应用服务器 上传...
    yu_liu阅读 1,828评论 0 0
  • 建立一级路由vue-router-demo1一个vue的单页应用(一级路由)的脚手架程序构建1.进入某个目录如D:...
    六年的承诺阅读 3,206评论 0 5
  • 本文包括:1、文件上传概述2、利用 Commons-fileupload 组件实现文件上传3、核心API——Dis...
    廖少少阅读 12,625评论 5 91
  • 她,着洁白洋装, 传说午夜十二点, 她就会重回世界, 杀掉孩子们,杀掉世界。 每次她都会说: 安息吧,人类, 至少...
    黑墨羽谜阅读 247评论 0 0
  • 以前娃老喜欢问我为什么呀,我不耐烦的时候就会说哪那么多为什么呀,十万个为什么。前几天,娃把书丢得满地都是,我就说你...
    只为你行走阅读 130评论 0 0