接收上傳的multi-file的文件(四)

構建工程

為例創建一個springmvc工程你需要spring-boot-starter-thymeleaf和 spring-boot-starter-web的起步依賴。為例能夠上傳文件在服務器,你需要在web.xml中加入標簽做相關的配置,但在sringboot 工程中,它已經為你自動做了,所以不需要你做任何的配置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<dependencies>
??????<dependency>
??????????<groupId>org.springframework.boot</groupId>
??????????<artifactId>spring-boot-starter-web</artifactId>
??????</dependency>
??????<dependency>
??????????<groupId>org.springframework.boot</groupId>
??????????<artifactId>spring-boot-starter-test</artifactId>
??????????<scope>test</scope>
??????</dependency>
??????<dependency>
??????????<groupId>org.springframework.boot</groupId>
??????????<artifactId>spring-boot-starter-thymeleaf</artifactId>
??????</dependency>
??</dependencies>

  

創建文件上傳controller

直接貼代碼:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
@Controller
public?class?FileUploadController {
????private?final?StorageService storageService;
????@Autowired
????public?FileUploadController(StorageService storageService) {
????????this.storageService = storageService;
????}
????@GetMapping("/")
????public?String listUploadedFiles(Model model)?throws?IOException {
????????model.addAttribute("files", storageService
????????????????.loadAll()
????????????????.map(path ->
????????????????????????MvcUriComponentsBuilder
????????????????????????????????.fromMethodName(FileUploadController.class,?"serveFile", path.getFileName().toString())
????????????????????????????????.build().toString())
????????????????.collect(Collectors.toList()));
????????return?"uploadForm";
????}
????@GetMapping("/files/{filename:.+}")
????@ResponseBody
????public?ResponseEntity<Resource> serveFile(@PathVariable?String filename) {
????????Resource file = storageService.loadAsResource(filename);
????????return?ResponseEntity
????????????????.ok()
????????????????.header(HttpHeaders.CONTENT_DISPOSITION,?"attachment; filename=\""+file.getFilename()+"\"")
????????????????.body(file);
????}
????@PostMapping("/")
????public?String handleFileUpload(@RequestParam("file") MultipartFile file,
???????????????????????????????????RedirectAttributes redirectAttributes) {
????????storageService.store(file);
????????redirectAttributes.addFlashAttribute("message",
????????????????"You successfully uploaded "?+ file.getOriginalFilename() +?"!");
????????return?"redirect:/";
????}
????@ExceptionHandler(StorageFileNotFoundException.class)
????public?ResponseEntity handleStorageFileNotFound(StorageFileNotFoundException exc) {
????????return?ResponseEntity.notFound().build();
????}
}

  

這個類通過@Controller注解,表明自己上一個Spring mvc的c。每個方法通過?
@GetMapping 或者@PostMapping注解表明自己的 http方法。

  • GET / 獲取已經上傳的文件列表
  • GET /files/{filename} 下載已經存在于服務器的文件
  • POST / 上傳文件給服務器

創建一個簡單的 html模板

為了展示上傳文件的過程,我們做一個界面:?
在src/main/resources/templates/uploadForm.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<html xmlns:th="http://www.thymeleaf.org">
<body>
????<div th:if="${message}">
????????<h2 th:text="${message}"/>
????</div>
????<div>
????????<form method="POST"?enctype="multipart/form-data"?action="/">
????????????<table>
????????????????<tr><td>File to upload:</td><td><input type="file"?name="file"?/></td></tr>
????????????????<tr><td></td><td><input type="submit"?value="Upload"?/></td></tr>
????????????</table>
????????</form>
????</div>
????<div>
????????<ul>
????????????<li th:each="file : ${files}">
????????????????<a th:href="${file}"?th:text="${file}"?/>
????????????</li>
????????</ul>
????</div>
</body></html>

  

上傳文件大小限制

如果需要限制上傳文件的大小也很簡單,只需要在springboot 工程的src/main/resources/application.properties 加入以下:

1
2
spring.http.multipart.max-file-size=128KB
spring.http.multipart.max-request-size=128KB

  

?

轉載于:https://www.cnblogs.com/MaxElephant/p/10231949.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/280929.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/280929.shtml
英文地址,請注明出處:http://en.pswp.cn/news/280929.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

數據庫讀寫分離 - MyBatis

2019獨角獸企業重金招聘Python工程師標準>>> 由于項目中數據量較大&#xff0c;訪問量也較高&#xff0c;故在數據庫的設計上&#xff0c;采用讀寫分離思想&#xff0c;達到性能要求&#xff01; 簡單科普一下實現讀寫分離的思路 配置及加載數據庫信息&#xff0c;即…

MySQL-03:數據表操作基本命令筆記

目錄 數據表 1、創建表 2、刪除表 3、清空表 4、修改表 5、基本數據類型 數據表 1、創建表 create table 表名(列名 類型 是否可以為空&#xff0c;列名 類型 是否可以為空 )ENGINEInnoDB DEFAULT CHARSETutf8 是否可空&#xff0c;null表示空&#xff0c;非字符串n…

java 怎么調試到第三方庫的內部,在有源碼的情況下

第一步&#xff0c; 把第三方庫加到workspace : https://stackoverflow.com/questions/370814/how-to-set-a-breakpoint-in-eclipse-in-a-third-party-library The most sure-fire way to do this (and end up with something thats actually useful) is to download the sou…

t-mobile頻段_T-Mobile再次被黑客入侵:超過200萬個帳號和地址可能泄漏

t-mobile頻段Attackers may have compromised three percent of T-Mobile’s 77 million customers on Monday, revealing personal information like addresses, phone numbers, and account numbers. 周一&#xff0c;攻擊者可能泄露了T-Mobile 7700萬客戶中的3&#xff05;&…

第二篇 第三章防火防煙分區檢查(一)

倉庫面積可以增加3倍 就是乘以4 要一定條件 : 第二篇 第三章防火防煙分區檢查&#xff08;一&#xff09; 21分鐘處 該題比較有代表性 停車庫 耐火等級允許最大面積 民用建筑防火分區 防煙分區的劃分    防火卷簾控制器的測試 防火閥 裝在通風,空調系統中 只有連在風機主管…

高斯數學

偉大的數學家高斯在9歲那年&#xff0c;用很短的時間完成了從1到100的累加。那原本是老師給學生們出的難題&#xff0c;希望他們能老老實實地待在教室里。高斯的方法很簡單&#xff0c;他發現這是50個101的求和&#xff1a;100&#xff0b;1、99&#xff0b;2、98&#xff0b;3…

Ant Design Blazor 發布 0.13.0,正式支持.NET 7!

時隔3個月&#xff0c;Ant Design Blazor 發布新功能版本 0.13.0&#xff0c;并正式支持.NET 7!大家快去訪問 antblazor.com 體驗吧&#xff01;&#x1f525; 新增 .NET 7 目標框架支持。#2810 ElderJames&#x1f525; 重構 Mentions 組件&#xff0c;修復定位和隱藏問題。#2…

gitlab 分支操作筆記\新建遠程分支\抓取遠程分支\復制遠程\刪除分支

密碼重新輸入與保存 git config --global http.emptyAuth truegit config --global credential.helper store 1.不復制遠程&#xff0c;直接新建遠程分支。&#xff08;非正規操作&#xff09; git init //初始化 git remote add origin http://****/*****/taskboard.git…

如何在Xbox One或PlayStation 4上為Skyrim特別版安裝Mods

The Elder Scrolls V: Skyrim Special Edition is now available on PlayStation 4 and Xbox One, and for the first time, “mods” are available to console gamers. Elder Scrolls V&#xff1a;Skyrim特別版現已在PlayStation 4和Xbox One上可用&#xff0c;并且首次向主…

微軟宣布:PowerBI 已經與 Office 整合,一切更簡單,變革又來了

很多人認為 Office 是 Office&#xff0c;PowerBI 是 PowerBI&#xff0c;怎么在 PPT 中顯示 PowerBI 呢&#xff1f;這種問題以后將再不會存在。微軟已經宣布&#xff0c;PowerBI 已經與 Office 深度整合&#xff0c;在未來的企業中&#xff0c;PowerBI 將與 Word&#xff0c;…

066:ORM查詢條件詳解-startswith和endswith:

ORM查詢條件詳解-startswith和endswith&#xff1a; startswith&#xff1a;判斷某個字段的值是否是以某個值開始的。大小寫敏感。示例代碼如下&#xff1a; articles1 Article.objects.filter(title__startswith"fuck") 以上代碼的意思是提取所有標題以 fuck 字符串…

前端工程師面試題匯總

HTML Doctype作用&#xff1f;嚴格模式與混雜模式如何區分&#xff1f;它們有何意義? HTML5 為什么只需要寫 <!DOCTYPE HTML>&#xff1f; 行內元素有哪些&#xff1f;塊級元素有哪些&#xff1f; 空(void)元素有那些&#xff1f; 頁面導入樣式時&#xff0c;使用lin…

MySQL-04:數據內容操作-增刪改查-基本命令筆記

1、增 insert into 表 (列名,列名...) values (值,值,值...) insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...) insert into 表 (列名,列名...) select (列名,列名...) from 表 2、刪 delete from 表 delete from 表 where id&#xff1d;1 and name&…

火狐和chrome_Firefox,Chrome和Edge都將支持WebAuthn的硬件兩因素身份驗證

火狐和chromeLogging into Gmail or Facebook could soon mean plugging in a USB device, potentially making phishing a thing of the past. 登錄Gmail或Facebook可能很快就意味著要插入USB設備&#xff0c;這可能使網絡釣魚成為過去。 That’s thanks to WebAuthn, a new o…

Could not delete .........May be locked by another process.

問題 原因&#xff1a;默認的設置是文件修改后立即發布&#xff0c;這樣的設置是在你每個保存文件時都會觸發&#xff0c;如果tomcat已經在運行&#xff0c;這樣頻繁的操作也會造成文件鎖死 解決&#xff1a; Tomcat 右鍵clean 轉載于:https://www.cnblogs.com/feiZhou/p/93…

flask的基礎1

1.python 現階段三大主流web框架Django Tornado Flask的對比 1.Django 主要特點是大而全,集成了很多組件,例如: Models Admin Form 等等, 不管你用得到用不到,反正它全都有,屬于全能型框架 2.Tornado 主要特點是原生異步非阻塞,在IO密集型應用和多任務處理上占據絕對性的優勢,屬…

python實現批量壓縮文件夾

前段時間碰到一個需要把目錄下文件夾壓縮的項目&#xff0c;但是度娘里沒找到&#xff0c;只好自己寫腳本了。 #coding:utf-8 import os filePath raw_input("請輸入路徑&#xff1a;") if filePath "":os._exit() #需要退出ds list(os.walk(filePath…

單元測試01:nunit 安裝與代碼測試

1.nunit 下載與安裝 a.下載 下載地址&#xff1a; http://nunit.org/download/ b.添加到系統環境變量 解壓下載包后&#xff0c;添加兩個路徑到環境變量&#xff0c;如&#xff1a; D:\nunitD:\nunit\nunit-console 2.建立測試項目 a.建立class project b.project 里re…

如何將您的Google Authenticator憑證移至新的Android手機或平板電腦

Most of the app data on your Android is probably synced online will automatically sync to a new phone or tablet. However, your Google Authenticator credentials won’t — they aren’t synchronized for obvious security reasons. Android上的大多數應用程序數據可…

關于經緯度的兩個計算[Teaksxgluxv]

一、子午線周長(公里) 40008.548 赤道周長(公里) 40075.704 緯度40008.548 / 360(度) 111.135 公里/度40008.548 / (360*60)(分) 1.85 公里/分40008.548 / (360*60*60)(秒) 30.87 米/秒 經度首先算相應經度位置的緯度圈長度40075.704 * cos(經度)然后方法相同&#xff0c;除…