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請(qǐng)求的最大大小,默認(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("有文件上傳請(qǐng)求進(jìn)來了");
FileOutputStream fileOutputStream = null;
InputStream inputStream = null;
try {
// 上傳文件是否存在
if (file != null && !file.isEmpty()) {
// 如果上傳文件存在,獲取它的原始文件名
String fileName = file.getOriginalFilename();
if (StringUtils.hasText(fileName)) {
// 將上傳文件存儲(chǔ)在服務(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)部會(huì)緩沖輸入,因此無需使用BufferedInputStream
* 大型流(超過2GB)將在復(fù)制完成后返回字節(jié)復(fù)制值-1 ,因?yàn)闊o法將正確的字節(jié)數(shù)作為int返回
* 對(duì)于大型流,需要使用copyLarge(InputStream, OutputStream)方法
* 參數(shù):
* input – 要讀取的InputStream
* output - 要寫入的OutputStream
* */
IOUtils.copy(inputStream, fileOutputStream);
}
}
else {
// 文件不存在
response.setStatus(HttpStatus.BAD_REQUEST.value());
return "文件不存在";
}
} catch (Exception e) {
// 文件上傳錯(cuò)誤
e.printStackTrace();
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
return "文件上傳錯(cuò)誤";
} finally {
if (fileOutputStream != null) {
fileOutputStream.flush();
fileOutputStream.close();
}
}
// 文件上傳成功
response.setStatus(HttpStatus.OK.value());
return "文件上傳成功";
}
}
啟動(dòng)類:
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)行測試。


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

上傳文件不存在。

控制臺(tái)的輸出。

到此這篇關(guān)于Java SpringBoot實(shí)現(xiàn)文件上傳功能的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot文件上傳內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java農(nóng)夫過河問題的繼承與多態(tài)實(shí)現(xiàn)詳解
這篇文章主要介紹了Java農(nóng)夫過河問題的繼承與多態(tài)實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
spring boot整合RabbitMQ實(shí)例詳解(Fanout模式)
這篇文章主要介紹了spring boot整合RabbitMQ的實(shí)例講解(Fanout模式),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-04-04
java代碼實(shí)現(xiàn)斗地主發(fā)牌功能
這篇文章主要介紹了java實(shí)現(xiàn)斗地主發(fā)牌功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
pdf2swf+flexpapers實(shí)現(xiàn)類似百度文庫pdf在線閱讀
這篇文章主要介紹了pdf2swf+flexpapers實(shí)現(xiàn)類似百度文庫pdf在線閱讀的相關(guān)資料,需要的朋友可以參考下2014-10-10
spring和quartz整合,并簡單調(diào)用(實(shí)例講解)
下面小編就為大家?guī)硪黄猻pring和quartz整合,并簡單調(diào)用(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07

