Java SpringBoot實(shí)現(xiàn)文件上傳功能的示例代碼
測試代碼
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.6.2</version>
</parent>
<packaging>jar</packaging>
<groupId>com.kaven</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot</name>
<description>springboot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties(配置文件):
spring.servlet.multipart.max-file-size=500MB
spring.servlet.multipart.max-request-size=500MB
max-file-size:指定允許上傳文件的最大大小,默認(rèn)值為1MB。
max-request-size:指定允許multipart/form-data請求的最大大小,默認(rèn)值為10MB。
上傳接口:
package com.kaven.springboot.controller;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@RestController
public class FilesController {
@PostMapping(value="/upload", headers="content-type=multipart/form-data")
public String upload(@RequestParam(value = "file") MultipartFile file,
HttpServletResponse response) throws IOException, InterruptedException {
System.out.println("有文件上傳請求進(jìn)來了");
FileOutputStream fileOutputStream = null;
InputStream inputStream = null;
try {
// 上傳文件是否存在
if (file != null && !file.isEmpty()) {
// 如果上傳文件存在,獲取它的原始文件名
String fileName = file.getOriginalFilename();
if (StringUtils.hasText(fileName)) {
// 將上傳文件存儲在服務(wù)器的E盤下(Windows)
java.io.File outFile = new java.io.File("E:\" + fileName);
// 基于outFile創(chuàng)建文件輸出流實(shí)例
fileOutputStream = new FileOutputStream(outFile);
// 獲取上傳文件的輸入流
inputStream = file.getInputStream();
/*
* 將字節(jié)從輸入流復(fù)制到輸出流
* 此方法在內(nèi)部會緩沖輸入,因此無需使用BufferedInputStream
* 大型流(超過2GB)將在復(fù)制完成后返回字節(jié)復(fù)制值-1 ,因為無法將正確的字節(jié)數(shù)作為int返回
* 對于大型流,需要使用copyLarge(InputStream, OutputStream)方法
* 參數(shù):
* input – 要讀取的InputStream
* output - 要寫入的OutputStream
* */
IOUtils.copy(inputStream, fileOutputStream);
}
}
else {
// 文件不存在
response.setStatus(HttpStatus.BAD_REQUEST.value());
return "文件不存在";
}
} catch (Exception e) {
// 文件上傳錯誤
e.printStackTrace();
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
return "文件上傳錯誤";
} finally {
if (fileOutputStream != null) {
fileOutputStream.flush();
fileOutputStream.close();
}
}
// 文件上傳成功
response.setStatus(HttpStatus.OK.value());
return "文件上傳成功";
}
}
啟動類:
package com.kaven.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(SpringbootApplication.class);
application.run(args);
}
}
使用Postman進(jìn)行測試。


上傳的文件是完整的,可以播放(視頻文件)。

上傳文件不存在。

控制臺的輸出。

到此這篇關(guān)于Java SpringBoot實(shí)現(xiàn)文件上傳功能的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot文件上傳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring+Junit4進(jìn)行接口測試實(shí)例代碼
這篇文章主要介紹了Spring+Junit4進(jìn)行接口測試實(shí)例代碼,涉及pom.xml、bean的配置,以及接口測試代碼等相關(guān)內(nèi)容,小編覺得還是挺不錯的,這里分享給大家,需要的朋友可以參考下2018-01-01
Java虛擬機(jī)內(nèi)存溢出與內(nèi)存泄漏
這篇文章主要介紹了Java虛擬機(jī)內(nèi)存溢出與內(nèi)存泄漏,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
java?面向?qū)ο蟠a塊及不同位置對屬性賦值的執(zhí)行順序
這篇文章主要介紹了java面向?qū)ο蟠a塊及不同位置對屬性賦值的執(zhí)行順序,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09
SpringBoot如何引入緩存提高單次查詢數(shù)據(jù)效率
這篇文章主要介紹了SpringBoot如何引入緩存提高單次查詢數(shù)據(jù)效率問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
swagger添加權(quán)限驗證保證API(接口)安全性(兩種方法)
這篇文章主要介紹了swagger添加權(quán)限驗證保證API(接口)安全性(兩種方法),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
Java多線程面試題之交替輸出問題的實(shí)現(xiàn)
本文主要介紹了Java多線程面試題之交替輸出問題的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01

