這篇文章主要講解了“SpringBoot怎么接收通過http上傳的multi-file的文件”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“SpringBoot怎么接收通過http上傳的multi-file的文件”吧!

成都創(chuàng)新互聯(lián)專注骨干網(wǎng)絡(luò)服務(wù)器租用十余年,服務(wù)更有保障!服務(wù)器租用,川西大數(shù)據(jù)中心 成都服務(wù)器租用,成都服務(wù)器托管,骨干網(wǎng)絡(luò)帶寬,享受低延遲,高速訪問。靈活、實(shí)現(xiàn)低成本的共享或公網(wǎng)數(shù)據(jù)中心高速帶寬的專屬高性能服務(wù)器。
接收通過http 上傳的multi-file的文件。
為例創(chuàng)建一個(gè)springmvc工程你需要spring-boot-starter-thymeleaf和 spring-boot-starter-web的起步依賴。為例能夠上傳文件在服務(wù)器,你需要在web.xml中加入
<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
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();
}
}這個(gè)類通過@Controller注解,表明自己上一個(gè)Spring mvc的c。每個(gè)方法通過
@GetMapping 或者@PostMapping注解表明自己的 http方法。
GET / 獲取已經(jīng)上傳的文件列表
GET /files/{filename} 下載已經(jīng)存在于服務(wù)器的文件
POST / 上傳文件給服務(wù)器
為了展示上傳文件的過程,我們做一個(gè)界面:
在src/main/resources/templates/uploadForm.html
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div th:if="${message}">
<h3 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>如果需要限制上傳文件的大小也很簡(jiǎn)單,只需要在springboot 工程的src/main/resources/application.properties 加入以下:
spring.http.multipart.max-file-size=128KB spring.http.multipart.max-request-size=128KB
感謝各位的閱讀,以上就是“SpringBoot怎么接收通過http上傳的multi-file的文件”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)SpringBoot怎么接收通過http上傳的multi-file的文件這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
文章題目:SpringBoot怎么接收通過http上傳的multi-file的文件
標(biāo)題網(wǎng)址:http://chinadenli.net/article46/goeheg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、App設(shè)計(jì)、網(wǎng)站建設(shè)、商城網(wǎng)站、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)