Java SpringBoot實現(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:指定允許上傳文件的最大大小,默認值為1MB。
max-request-size:指定允許multipart/form-data請求的最大大小,默認值為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("有文件上傳請求進來了");
FileOutputStream fileOutputStream = null;
InputStream inputStream = null;
try {
// 上傳文件是否存在
if (file != null && !file.isEmpty()) {
// 如果上傳文件存在,獲取它的原始文件名
String fileName = file.getOriginalFilename();
if (StringUtils.hasText(fileName)) {
// 將上傳文件存儲在服務器的E盤下(Windows)
java.io.File outFile = new java.io.File("E:\" + fileName);
// 基于outFile創(chuàng)建文件輸出流實例
fileOutputStream = new FileOutputStream(outFile);
// 獲取上傳文件的輸入流
inputStream = file.getInputStream();
/*
* 將字節(jié)從輸入流復制到輸出流
* 此方法在內(nèi)部會緩沖輸入,因此無需使用BufferedInputStream
* 大型流(超過2GB)將在復制完成后返回字節(jié)復制值-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進行測試。


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

上傳文件不存在。

控制臺的輸出。

到此這篇關于Java SpringBoot實現(xiàn)文件上傳功能的示例代碼的文章就介紹到這了,更多相關SpringBoot文件上傳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java農(nóng)夫過河問題的繼承與多態(tài)實現(xiàn)詳解
這篇文章主要介紹了Java農(nóng)夫過河問題的繼承與多態(tài)實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-01-01
spring boot整合RabbitMQ實例詳解(Fanout模式)
這篇文章主要介紹了spring boot整合RabbitMQ的實例講解(Fanout模式),非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-04-04
pdf2swf+flexpapers實現(xiàn)類似百度文庫pdf在線閱讀
這篇文章主要介紹了pdf2swf+flexpapers實現(xiàn)類似百度文庫pdf在線閱讀的相關資料,需要的朋友可以參考下2014-10-10

