這篇文章主要講解了“SpringBoot怎么接收通過http上傳的multi-file的文件”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“SpringBoot怎么接收通過http上傳的multi-file的文件”吧!
成都創(chuàng)新互聯(lián)專注骨干網(wǎng)絡(luò)服務(wù)器租用十余年,服務(wù)更有保障!服務(wù)器租用,川西大數(shù)據(jù)中心 成都服務(wù)器租用,成都服務(wù)器托管,骨干網(wǎng)絡(luò)帶寬,享受低延遲,高速訪問。靈活、實現(xiàn)低成本的共享或公網(wǎng)數(shù)據(jù)中心高速帶寬的專屬高性能服務(wù)器。
接收通過http 上傳的multi-file的文件。
為例創(chuàng)建一個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(); } }
這個類通過@Controller注解,表明自己上一個Spring mvc的c。每個方法通過
@GetMapping 或者@PostMapping注解表明自己的 http方法。
GET / 獲取已經(jīng)上傳的文件列表
GET /files/{filename} 下載已經(jīng)存在于服務(wù)器的文件
POST / 上傳文件給服務(wù)器
為了展示上傳文件的過程,我們做一個界面:
在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>
如果需要限制上傳文件的大小也很簡單,只需要在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)過本文的學習后,相信大家對SpringBoot怎么接收通過http上傳的multi-file的文件這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
文章題目:SpringBoot怎么接收通過http上傳的multi-file的文件
標題網(wǎng)址:http://bm7419.com/article46/goeheg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、App設(shè)計、網(wǎng)站建設(shè)、商城網(wǎng)站、網(wǎng)站設(shè)計公司、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)