前言
本篇文章主要介紹的是SpringBoot實現文件上傳下載。
GitHub源碼鏈接位於文章底部。
創建maven項目,在pom文件中添加依賴
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<!--父級依賴,它用來提供相關的 Maven 默認依賴。使用它之后,常用的springboot包依賴可以省去version 標簽-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath ></relativePath>
</parent>
<dependencies>
<!-- Spring Boot Web 依賴 核心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
</dependencies>
application.yml文件中添加配置
server:
port: 8080
spring:
servlet:
multipart:
enabled: true
# 最大支持文件大小
max-file-size: 100MB
# 最大支持請求大小
max-request-size: 100MB
#文件存儲路徑
filepath: D:/file/
文件存儲路徑可以修改,文件上傳的大小限制這里設置的100M
controller層
在controller文件夾下創建FileController在controller文件夾下創建FileController
因為這里只進行文件上傳下載,不進行業務邏輯處理,因此只用在控制層實現即可,定義文件上傳、下載的接口。
@RestController
@RequestMapping("/file")
@Slf4j
public class FileController {
@Value("${filepath}")
private String filepath;
/**
* 處理文件上傳
*/
@PostMapping(value = "/upload")
public String uploading(@RequestParam("file") MultipartFile file) {
File targetFile = new File(filepath);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
try (FileOutputStream out = new FileOutputStream(filepath + file.getOriginalFilename());){
out.write(file.getBytes());
} catch (Exception e) {
e.printStackTrace();
log.error("文件上傳失敗!");
return "uploading failure";
}
log.info("文件上傳成功!");
return "uploading success";
}
@RequestMapping("/download")
public void downLoad(HttpServletResponse response) throws UnsupportedEncodingException {
String filename="JAVA核心知識點整理.pdf";
String filePath = "D:/file" ;
File file = new File(filePath + "/" + filename);
if(file.exists()){
response.setContentType("application/octet-stream");
response.setHeader("content-type", "application/octet-stream");
response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(filename,"utf8"));
byte[] buffer = new byte[1024];
//輸出流
OutputStream os = null;
try(FileInputStream fis= new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);) {
os = response.getOutputStream();
int i = bis.read(buffer);
while(i != -1){
os.write(buffer);
i = bis.read(buffer);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
html頁面進行測試
在resource下創建static文件夾,在static文件夾下創建file.html
springboot項目中,靜態資源默認訪問目錄就是resource下的static文件夾
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文件上傳</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="file/upload">
<input type="file" name="file"/>
<input type="submit" value="上傳"/>
</form>
<a href="/file/download">文件下載</a>
</body>
</html>
啟動類:
在controller同級的目錄下創建啟動類FileApplication
@SpringBootApplication
public class FileApplication {
public static void main(String[] args) {
SpringApplication.run(FileApplication.class, args);
}
}
測試:運行啟動類,訪問localhost:8080/file.html ,進行相關操作。
本文GitHub源碼:https://github.com/lixianguo5097/springboot/tree/master/springboot-file
CSDN:https://blog.csdn.net/qq_27682773
簡書:https://www.jianshu.com/u/e99381e6886e
博客園:https://www.cnblogs.com/lixianguo
個人博客:https://www.lxgblog.com