Spring Boot 2.x 实现文件上传
简述
此教程是一个日常上传文件的脚手架,用到的很多,就写一个简单的Demo,为了日后更加方便的CV编程,有紧急需求的可直接去源码CV
源码地址
https://github.com/Gleans/SpringBootLearn/tree/master/springboot-upfile
新建项目
目录
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-upfile</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-upfile</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Thymeleaf组件 动态页面-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
UploadServiceImpl.java
package com.example.springbootupfile.service.impl;
import com.example.springbootupfile.service.UploadService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.Map;
@Service
public class UploadServiceImpl implements UploadService {
@Value("${file.path}")
private String path;
@Override
public Map<Object, Object> uploadFile(MultipartFile file) throws IOException {
Map<Object, Object> resultMap = new HashMap<>(4);
if (file.isEmpty()) {
resultMap.put("code", -1);
resultMap.put("msg", "文件不能为空");
}
// 得到上传时的文件du名
String fileName = file.getOriginalFilename();
// 获取文件输入流
InputStream is = file.getInputStream();
/*
* 使用 Files.copy 命令将文件从输入流源复制到目标目录
* StandardCopyOption.REPLACE_EXISTING 覆盖已有的目标路径
*/
Files.copy(is, Paths.get(path + fileName), StandardCopyOption.REPLACE_EXISTING);
resultMap.put("code", 0);
resultMap.put("msg", "success");
return resultMap;
}
}
自行创建一个 UploadService
接口
简单的前端上传页面
templates/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>上传文件</h1>
<form action="/upload" method="post" enctype="multipart/form-data">
<label>选择文件</label>
<input type="file" name="file">
<button type="submit">上传</button>
</form>
</body>
</html>
templates/result.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>返回页面</title>
</head>
<body>
<h1>返回结果</h1>
<p>返回代码:<span th:text="${code}"></span></p>
<p>message:<span th:text="${msg}"></span></p>
</body>
</html>
配置文件
配置上传的路径
file.path=D:baidu
启动类
SpringbootUpfileApplication.java
package com.example.springbootupfile;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootUpfileApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootUpfileApplication.class, args);
}
}
启动项目
上传页面
返回页面