SpringBoot.08.SpringBoot檔案上傳與下載下傳
-
- 前言
- 檔案上傳
-
- 1.建立Module
- 2.項目配置
- 3.pom.xml
- 4.application-dev.yml
- 5.設定Working directory
- 6.upload.jsp
- 7.File相關
-
- 7.1 FileController.java
- 7.2 FileConstants.java
- 7.3 FileUtils.java
- 8.MyWebMvcConfigurer.java
- 9.測試
-
- 9.1 windows下
- 9.2 linux下
- 檔案下載下傳
-
- 1.download.jsp
- 2.FIleController.java
- 3.FileUtils.java
- 4.測試
-
- 4.1 windows下
- 4.2 linux下
前言
檔案操作包括
檔案上傳
和
檔案下載下傳
。
使用者将自己計算機中檔案上傳到項目所在伺服器|檔案伺服器|OSS的過程 稱之為檔案上傳;檔案下載下傳則相反。下面我們先來看下檔案上傳
檔案上傳
由于本次案例會涉及到頁面(jsp)操作,是以不要忘記添加配置jsp相關的東西
1.建立Module
建立Module -
springboot-07-file
,按照下圖所示填寫資訊:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIjYTMfhHLlN3XnxCM38FdsYkRGZkRG9lcvx2bjxCMy8VZ6l2csY2U1UTSXlVNRdkc1UnMyVTQClGVF5UMR9Fd4VGdsATNfd3bkFGazxycykFaKdkYzZUbapXNXlleSdVY2pESa9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnLiZDZkRTYlZWZwEmN3EjZ4kTY0QzMwMDMjVWOhFzYiFzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
點選下一步選擇依賴
Spring Web
,點選
Finish
。如下圖所示:
2.項目配置
我們在
springboot-06-AOP
中的代碼上進行改造。首先我們規整一下項目,然後将
springboot-06-AOP
中的代碼拷貝過來,去除AOP相關的代碼。如下圖所示:
3.pom.xml
pom.xml
檔案中新增了jsp有關的依賴
<?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.5.0</version>
<relativePath/>
</parent>
<groupId>com.christy</groupId>
<artifactId>springboot-07-file</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-07-file</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.6</version>
</dependency>
<!-- mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- mybatis-spring-boot-starter
由于springboot整合mybatis版本中預設依賴mybatis 是以不需要額外引入mybati版本,否則會出現沖突
-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!-- 每次建立的項目如果需要開啟熱部署都需要引入該依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- c标簽庫 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 讓内嵌tomcat具有解析jsp功能 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!-- 檔案上傳相關 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
<exclusions>
<exclusion>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 一定要注意mvn的插件一定是1.4.2.RELEASE,否則jsp通路不到 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.2.RELEASE</version>
</plugin>
</plugins>
<resources>
<!-- 打包時将jsp檔案拷貝到META-INF目錄下-->
<resource>
<!-- 指定resources插件處理哪個目錄下的資源檔案 -->
<directory>src/main/webapp</directory>
<!-- 注意必須要放在此目錄下才能被通路到 -->
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/**</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
4.application-dev.yml
server:
port: 8807
# 修改jsp無須重新開機應用
servlet:
jsp:
init-parameters:
development: true
# datasource
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/christy?characterEncoding=UTF-8
username: root
password: 123456
# 配置視圖解析器
mvc:
view:
prefix: / #這個代表jsp的根路徑 需要在java包下建立webapp目錄
suffix: .jsp
# 配置檔案上傳大小
servlet:
multipart:
max-request-size: 10MB # 設定請求的資料最大為10MB
max-file-size: 10MB # 設定能夠處理的單個檔案最大值為10MB
# mybatis
mybatis:
mapper-locations: classpath:com/christy/mapper/*.xml
type-aliases-package: com.christy.entity
# logback
# logging:
# level:
# root: info # 指定全局根日志級别為INFO(預設)
# com.christy.mapper: debug # 指定包級别日志為debug,在控制台不列印DEBUG日志時能看到sql語句
# 多環境下我們需要指定目前使用的日志檔案
logging:
config: classpath:logs/logback-spring-dev.xml
# 檔案上傳路徑
file:
# window下檔案上傳路徑
windows:
path: G:/files/
# linux下檔案上傳路徑
linux:
path: /usr/files/
# 虛拟路徑
virtual:
path: /files/
有關配置相關說明
1.設定檔案上傳大小
SpringBoot預設上傳大小是1048576 bytes,也就是1MB(也有說是10MB的,但是我的是1MB),這裡改為10MB,否則會報如下錯誤:# 配置檔案上傳大小 servlet: multipart: max-request-size: 10MB # 設定請求的資料最大為10MB max-file-size: 10MB # 設定能夠處理的單個檔案最大值為10MB
![]()
SpringBoot.08.SpringBoot檔案上傳與下載下傳
2.設定檔案上傳路徑及虛拟路徑
# 檔案上傳路徑 file: # window下檔案上傳路徑 windows: path: G:/files/ # linux下檔案上傳路徑 linux: path: /usr/files/ # 虛拟路徑 virtual: path: /files/
5.設定Working directory
我們選擇
Edit Configuration
,在
Working directory
中選擇
MODULE_DIR
,然後點選
Apply
->
OK
。如下圖所示:
6.upload.jsp
<%@ page pageEncoding="UTF-8" language="java" contentType="text/html; UTF-8" %>
<!doctype html>
<html >
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>springboot檔案上傳</title>
</head>
<body>
<h1>檔案上傳</h1>
<%--
method:post 含有檔案上傳的頁面送出方式必須是POST
enctype:該屬性規定在發送到伺服器之前應該如何對表單資料進行編碼
預設地,表單資料會編碼為 "application/x-www-form-urlencoded"。就是說,在發送到伺服器之前,
所有字元都會進行編碼(空格轉換為 "+" 加号,特殊符号轉換為 ASCII HEX 值)。但是該種方式不能編碼檔案
在有檔案的情況下,該值要修改為:multipart/form-data
--%>
<form action="${pageContext.request.contextPath}/file/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="上傳">
</form>
</body>
</html>
7.File相關
7.1 FileController.java
import com.christy.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* @Author Christy
* @Date 2021/9/14 16:28
**/
@Controller
@RequestMapping("/file")
public class FileController {
private FileUtils fileUtils;
@Autowired
public FileController(FileUtils fileUtils) {
this.fileUtils = fileUtils;
}
/**
* MultipartFile file: 檔案接收對象 file變量名要與form表單中input type="file"标簽中的name屬性一緻
*
* @author Christy
* @date 2021/9/15 13:57
* @param request
* @return java.lang.String
*/
@RequestMapping("/upload")
@ResponseBody
public String upload(HttpServletRequest request) {
try {
fileUtils.fileUpload(request);
} catch (IOException e) {
e.printStackTrace();
return "檔案上傳失敗";
}
return "檔案上傳成功";
}
}
7.2 FileConstants.java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @Author Christy
* @Date 2021/9/15 16:35
**/
@Component
public class FileConstants {
/**
* windows檔案上傳儲存的位址
*/
public static String fileWindowsPath;
/**
* linux檔案上傳儲存的位址
*/
public static String fileLinuxPath;
/**
* 檔案上傳虛拟路徑
*/
public static String fileVirtualPath;
@Value("${file.windows.path}")
public void setFileWindowsPath(String w) {
fileWindowsPath = w;
}
@Value("${file.linux.path}")
public void setFileLinuxPath(String l) {
fileLinuxPath = l;
}
@Value("${file.virtual.path}")
public void setFileVirtualPath(String v) {
fileVirtualPath = v;
}
}
7.3 FileUtils.java
import com.christy.constants.FileConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.UUID;
/**
* @Author Christy
* @Date 2021/9/15 14:29
**/
@Component
@Slf4j
public class FileUtils {
public String getRealPath(){
String filePath = "";
String os = System.getProperty("os.name");
if(os.toLowerCase().startsWith("win")){
filePath = FileConstants.fileWindowsPath;
} else {
filePath = FileConstants.fileLinuxPath;
}
File file = new File(filePath);
if(!file.exists() || !file.isDirectory()){
file.mkdirs();
}
return filePath;
}
public void fileUpload(HttpServletRequest request) throws IOException {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
if (multipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator<String> iterator = multiRequest.getFileNames();
while (iterator.hasNext()) {
// 上傳的檔案
MultipartFile item = multiRequest.getFile(iterator.next());
if (item != null) {
log.info("檔案名:" + item.getOriginalFilename());
log.info("檔案類型:" + item.getContentType());
log.info("檔案大小:" + item.getSize());
// 檔案的原始名稱
String originalFilename = item.getOriginalFilename();
String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
// 如果名稱不為“”,說明該檔案存在,否則說明該檔案不存在
if (!"".equals(originalFilename.trim())) {
String newFileName = UUID.randomUUID() + ext;
String filePath = getRealPath();
//不是圖檔,直接儲存
item.transferTo(new File(filePath, newFileName));
}
}
}
}
}
}
8.MyWebMvcConfigurer.java
import com.christy.constants.FileConstants;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @Author Christy
* @Date 2021/9/15 16:24
**/
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
/**
* 檔案上傳到伺服器後并不能在項目中直接通路,需要将磁盤路徑映射成虛拟路徑使用http://domain/的方式才可以
*/
String os = System.getProperty("os.name");
if(os.toLowerCase().startsWith("win")){
// Windows虛拟路徑映射本地磁盤
registry.addResourceHandler(FileConstants.fileVirtualPath + "**").addResourceLocations("file:" + FileConstants.fileWindowsPath);
} else {
// Linux虛拟路徑映射本地磁盤
registry.addResourceHandler(FileConstants.fileVirtualPath + "**").addResourceLocations("file:" + FileConstants.fileLinuxPath);
}
}
}
9.測試
9.1 windows下
我們啟動項目,浏覽器打開
upload.jsp
頁面,我們選擇一張圖檔。如下所示:
點選上傳按鈕,注意觀察控制台和頁面。成功的情況如下所示:
我們在浏覽器中通路
http://localhost:8807/files/e8fdb612-1fe8-4e4c-a3df-04297eef0267.jpg
,圖檔能夠正常加載。如下圖所示:
9.2 linux下
我們啟動虛拟機
Christy(BaseOS)
,IP位址為
192.168.8.100
。進入到
/usr/apps/
目錄下,上傳jar包至該目錄。如下圖所示:
執行
java -jar springboot-07-file-0.0.1-SNAPSHOT.jar
指令,啟動該項目。如下圖所示:
我們在浏覽器中通路
http://192.168.8.100:8807/upload.jsp
,還是選擇上面的圖檔。如下圖所示:
點選上傳,頁面傳回
檔案上傳成功
,控制台也能夠列印輸出檔案資訊。如下圖所示:
我們回到目錄
/usr/files
,可以找到剛才上傳的圖檔。如下圖所示:
我們 浏覽器通路
http://192.168.8.100:8807/files/29b3b42c-f72f-462a-b6f9-d19afcfeab1c.jpg
,圖檔也可以正常加載。如下圖所示:
檔案下載下傳
相比較于檔案上傳,檔案下載下傳就簡單多了。首先我們要提供檔案的下載下傳位址,就拿上面我們上傳的照片來說,他的通路位址就是我們的下載下傳位址
在Windows中的下載下傳位址:
http://localhost:8807/files/e8fdb612-1fe8-4e4c-a3df-04297eef0267.jpg
在Linux中的下載下傳位址:
http://192.168.8.100:8807/files/29b3b42c-f72f-462a-b6f9-d19afcfeab1c.jpg
1.download.jsp
<%@ page pageEncoding="UTF-8" language="java" contentType="text/html; UTF-8" %>
<!doctype html>
<html >
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>springboot檔案下載下傳</title>
</head>
<body>
<h1>檔案下載下傳</h1>
<%--
a标簽裡面的filePath就是我們最終儲存到資料庫中的檔案路徑,這裡不要搞錯了
--%>
<a
href="${pageContext.request.contextPath}/file/download?fileName=e8fdb612-1fe8-4e4c-a3df-04297eef0267.jpg&filePath=/files/e8fdb612-1fe8-4e4c-a3df-04297eef0267.jpg" target="_blank" rel="external nofollow" >
睡美人-windows
</a>
<br/>
<a
href="${pageContext.request.contextPath}/file/download?fileName=29b3b42c-f72f-462a-b6f9-d19afcfeab1c.jpg&filePath=/files/29b3b42c-f72f-462a-b6f9-d19afcfeab1c.jpg" target="_blank" rel="external nofollow" >
睡美人-linux</a>
</body>
</html>
2.FIleController.java
import com.christy.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @Author Christy
* @Date 2021/9/14 16:28
**/
@RestController
@RequestMapping("/file")
public class FileController {
private FileUtils fileUtils;
@Autowired
public FileController(FileUtils fileUtils) {
this.fileUtils = fileUtils;
}
/**
* MultipartFile file: 檔案接收對象 file變量名要與form表單中input type="file"标簽中的name屬性一緻
*
* @author Christy
* @date 2021/9/15 13:57
* @param request
* @return java.lang.String
*/
@RequestMapping("/upload")
public String upload(HttpServletRequest request) {
try {
fileUtils.fileUpload(request);
} catch (IOException e) {
e.printStackTrace();
return "檔案上傳失敗";
}
return "檔案上傳成功";
}
@RequestMapping("/download")
public String download(HttpServletResponse response, String fileName, String filePath){
try {
return fileUtils.fileDownload(response, fileName, filePath);
} catch (IOException e) {
e.printStackTrace();
return "檔案下載下傳失敗!";
}
}
}
3.FileUtils.java
import com.christy.constants.FileConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.UUID;
/**
* @Author Christy
* @Date 2021/9/15 14:29
**/
@Component
@Slf4j
public class FileUtils {
/**
* 擷取檔案上傳的真實路徑
* @author Christy
* @date 2021/9/17 10:02
* @param
* @return java.lang.String
*/
public String getUploadRealPath(){
String filePath = "";
String os = System.getProperty("os.name");
if(os.toLowerCase().startsWith("win")){
filePath = FileConstants.fileWindowsPath;
} else {
filePath = FileConstants.fileLinuxPath;
}
File file = new File(filePath);
if(!file.exists() || !file.isDirectory()){
file.mkdirs();
}
return filePath;
}
/**
* 擷取檔案下載下傳的真實路徑
* @author Christy
* @date 2021/9/17 10:02
* @param
* @return java.lang.String
*/
public String getDownloadRealPath(String filePath){
String realPath = "";
if(filePath.indexOf(FileConstants.fileVirtualPath) == 0){
String os = System.getProperty("os.name");
// 将檔案的虛拟路徑替換成實體路徑
if(os.toLowerCase().startsWith("win")){
StringBuilder stringbuilder = new StringBuilder(filePath);
stringbuilder.replace(0, FileConstants.fileVirtualPath.length(), FileConstants.fileWindowsPath);
realPath = stringbuilder.toString();
} else {
StringBuilder stringbuilder = new StringBuilder(filePath);
stringbuilder.replace(0, FileConstants.fileVirtualPath.length(), FileConstants.fileLinuxPath);
realPath = stringbuilder.toString();
}
}
return realPath;
}
public void fileUpload(HttpServletRequest request) throws IOException {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
if (multipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator<String> iterator = multiRequest.getFileNames();
while (iterator.hasNext()) {
// 上傳的檔案
MultipartFile item = multiRequest.getFile(iterator.next());
if (item != null) {
log.info("檔案名:" + item.getOriginalFilename());
log.info("檔案類型:" + item.getContentType());
log.info("檔案大小:" + item.getSize());
// 檔案的原始名稱
String originalFilename = item.getOriginalFilename();
String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
// 如果名稱不為“”,說明該檔案存在,否則說明該檔案不存在
if (!"".equals(originalFilename.trim())) {
String newFileName = UUID.randomUUID() + ext;
String filePath = getUploadRealPath();
//不是圖檔,直接儲存
item.transferTo(new File(filePath, newFileName));
}
}
}
}
}
public String fileDownload(HttpServletResponse response, String fileName, String filePath) throws IOException {
log.info("目前下載下傳檔案名為: {}",fileName);
log.info("目前下載下傳檔案目錄: {}",filePath);
// 擷取檔案的真實存儲路徑
String realPath = getDownloadRealPath(filePath);
File file = new File(realPath);
if(file.exists()){
// 将檔案讀取為檔案輸入流
FileInputStream is = new FileInputStream(file);
// 擷取響應流之前 一定要設定以附件形式下載下傳 attachment:附件
response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode(fileName,"UTF-8"));
// 擷取響應輸出流
ServletOutputStream os = response.getOutputStream();
// 輸入流複制給輸出流
/*int len=0;
byte[] b = new byte[1024];
while(true){
len = is.read(b);
if(len==-1) break;
os.write(b,0,len);
}*/
FileCopyUtils.copy(is,os);
log.info("檔案下載下傳成功!");
} else {
return "檔案不存在";
}
return "檔案下載下傳成功!";
}
}
為了差別于下載下傳時擷取屋裡路徑的方法,這裡将上傳時擷取真實路徑的方法改成了
getDownloadRealPath()
。
getUploadRealPath()
4.測試
4.1 windows下
我們啟動項目,在浏覽器通路
http://localhost:8807/download.jsp
,然後在界面中點選
睡美人-windows
下載下傳圖檔(這裡不要點選linux下的),注意觀察界面與控制台。如下圖所示:
4.2 linux下
我們重新打包目前項目,傳至linux伺服器上,執行指令
java -jar springboot-07-file-0.0.1-SNAPSHOT.jar
重新啟動該項目。如下圖所示:
然後浏覽器通路
http://192.168.8.100:8807/download.jsp
,點選
睡美人-linux
,注意觀察頁面和控制台。如下圖所示: