一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 使用Spring Boot上傳文件功能

使用Spring Boot上傳文件功能

2021-03-18 14:20純潔的微笑 Java教程

上傳文件是互聯(lián)網(wǎng)中常應(yīng)用的場(chǎng)景之一,最典型的情況就是上傳頭像等,今天就帶著大家做一個(gè)Spring Boot上傳文件的小案例,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧

上傳文件是互聯(lián)網(wǎng)中常常應(yīng)用的場(chǎng)景之一,最典型的情況就是上傳頭像等,今天就帶著帶著大家做一個(gè)spring boot上傳文件的小案例。

1、pom包配置

我們使用spring boot最新版本1.5.9、jdk使用1.8、tomcat8.0。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<parent>
 <groupid>org.springframework.boot</groupid>
 <artifactid>spring-boot-starter-parent</artifactid>
 <version>1.5.9.release</version>
</parent>
<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.springframework.boot</groupid>
 <artifactid>spring-boot-starter-thymeleaf</artifactid>
 </dependency>
 <dependency>
 <groupid>org.springframework.boot</groupid>
 <artifactid>spring-boot-devtools</artifactid>
 <optional>true</optional>
 </dependency>
</dependencies>

引入了 spring-boot-starter-thymeleaf 做頁面模板引擎,寫一些簡(jiǎn)單的上傳示例。

2、啟動(dòng)類設(shè)置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@springbootapplication
public class fileuploadwebapplication {
 public static void main(string[] args) throws exception {
 springapplication.run(fileuploadwebapplication.class, args);
 }
 //tomcat large file upload connection reset
 @bean
 public tomcatembeddedservletcontainerfactory tomcatembedded() {
 tomcatembeddedservletcontainerfactory tomcat = new tomcatembeddedservletcontainerfactory();
 tomcat.addconnectorcustomizers((tomcatconnectorcustomizer) connector -> {
  if ((connector.getprotocolhandler() instanceof abstracthttp11protocol<?>)) {
  //-1 means unlimited
  ((abstracthttp11protocol<?>) connector.getprotocolhandler()).setmaxswallowsize(-1);
  }
 });
 return tomcat;
 }
}

tomcatembedded這段代碼是為了解決,上傳文件大于10m出現(xiàn)連接重置的問題。此異常內(nèi)容globalexception也捕獲不到。

使用Spring Boot上傳文件功能

詳細(xì)內(nèi)容參考: tomcat large file upload connection reset

3、編寫前端頁面

上傳頁面

?
1
2
3
4
5
6
7
8
9
10
<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<h1>spring boot file upload example</h1>
<form method="post" action="/upload" enctype="multipart/form-data">
 <input type="file" name="file" /><br/><br/>
 <input type="submit" value="submit" />
</form>
</body>
</html>

非常簡(jiǎn)單的一個(gè)post請(qǐng)求,一個(gè)選擇框選擇文件,一個(gè)提交按鈕,效果如下:

使用Spring Boot上傳文件功能

上傳結(jié)果展示頁面:

?
1
2
3
4
5
6
7
8
9
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>
<h1>spring boot - upload status</h1>
<div th:if="${message}">
 <h2 th:text="${message}"/>
</div>
</body>
</html>

效果圖如下:

使用Spring Boot上傳文件功能

4、編寫上傳控制類

訪問localhost自動(dòng)跳轉(zhuǎn)到上傳頁面:

?
1
2
3
4
@getmapping("/")
public string index() {
 return "upload";
}

上傳業(yè)務(wù)處理

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@postmapping("/upload")
public string singlefileupload(@requestparam("file") multipartfile file,
    redirectattributes redirectattributes) {
 if (file.isempty()) {
 redirectattributes.addflashattribute("message", "please select a file to upload");
 return "redirect:uploadstatus";
 }
 try {
 // get the file and save it somewhere
 byte[] bytes = file.getbytes();
 path path = paths.get(uploaded_folder + file.getoriginalfilename());
 files.write(path, bytes);
 redirectattributes.addflashattribute("message",
  "you successfully uploaded '" + file.getoriginalfilename() + "'");
 } catch (ioexception e) {
 e.printstacktrace();
 }
 return "redirect:/uploadstatus";
}

上面代碼的意思就是,通過 multipartfile 讀取文件信息,如果文件為空跳轉(zhuǎn)到結(jié)果頁并給出提示;如果不為空讀取文件流并寫入到指定目錄,最后將結(jié)果展示到頁面。

multipartfile 是spring上傳文件的封裝類,包含了文件的二進(jìn)制流和文件屬性等信息,在配置文件中也可對(duì)相關(guān)屬性進(jìn)行配置,基本的配置信息如下:

?
1
2
3
4
5
spring.http.multipart.enabled=true #默認(rèn)支持文件上傳.
spring.http.multipart.file-size-threshold=0 #支持文件寫入磁盤.
spring.http.multipart.location= # 上傳文件的臨時(shí)目錄
spring.http.multipart.max-file-size=1mb # 最大支持文件大小
spring.http.multipart.max-request-size=10mb # 最大支持請(qǐng)求大小

最常用的是最后兩個(gè)配置內(nèi)容,限制文件上傳大小,上傳時(shí)超過大小會(huì)拋出異常:

使用Spring Boot上傳文件功能

更多配置信息參考這里: common application properties

5、異常處理

?
1
2
3
4
5
6
7
8
9
@controlleradvice
public class globalexceptionhandler {
 
 @exceptionhandler(multipartexception.class)
 public string handleerror1(multipartexception e, redirectattributes redirectattributes) {
 redirectattributes.addflashattribute("message", e.getcause().getmessage());
 return "redirect:/uploadstatus";
 }
}

 

設(shè)置一個(gè) @controlleradvice 用來監(jiān)控 multipart 上傳的文件大小是否受限,當(dāng)出現(xiàn)此異常時(shí)在前端頁面給出提示。利用 @controlleradvice 可以做很多東西,比如全局的統(tǒng)一異常處理等,感興趣的同學(xué)可以下來了解。

6、總結(jié)

這樣一個(gè)使用spring boot上傳文件的簡(jiǎn)單demo就完成了,感興趣的同學(xué)可以將示例代碼下載下來試試吧。

參考:

示例代碼-github

示例代碼-碼云

總結(jié)

以上所述是小編給大家介紹的使用spring boot上傳文件功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!

原文鏈接:http://www.ityouknow.com/

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: jk制服白丝超短裙流白浆 | 国产视频99| 99草视频| 日本理论片中文在线观看2828 | 日本黄色网页 | 无码国产成人777爽死在线观看 | 91混血大战上海双胞胎 | 欧美日韩国产在线人成dvd | 亚洲www视频 | 欧美丰满大乳大屁在线观看股 | 全彩成人18h漫画 | 久久aa毛片免费播放嗯啊 | 久久香蕉电影 | 高清日韩在线 | 国产亚洲精品美女久久久 | 天天草天天| 国产伦精品一区二区三区免 | 亚洲精品国产AV成人毛片 | 色综合伊人色综合网亚洲欧洲 | 日本大学jalapsiki| 蜜月aⅴ免费一区二区三区 蜜桃影像传媒推广 | 91制片厂果冻星空传媒3xg | 脱了白丝校花的内裤猛烈进入 | 亚洲国产精品线在线观看 | 免费毛片大全 | 67194在线免费观看 | 男人女人性生活视频 | 国产欧美一区二区精品性色99 | 欧美久在线观看在线观看 | 天堂资源在线8 | 国产成人久久精品一区二区三区 | 黄情视频 | 99久久无色码中文字幕 | 国产美女亚洲精品久久久综合 | yellow高清视频日本动漫 | 女教师被学生糟蹋三天 | 国内精品久久久久香蕉 | ck7788免费视频| 日韩欧美天堂 | 荡女淫春2未删减版 | 美女的隐私脱裤子无遮挡 |