บทความนี้แนะนำฟังก์ชั่นการอัปโหลดและดาวน์โหลดของ Springboot
1. สร้างโครงการ Springboot และเพิ่มการพึ่งพา
Compile ("org.springframework.boot: Spring-Boot-Starter-Web") Compile ("org.springframework.boot: Spring-Boot-Starter-Thymeleaf")ไดเรกทอรีโครงการคือ:
Application.java คลาสเริ่มต้น
แพคเกจสวัสดี; นำเข้า org.springframework.boot.springapplication; นำเข้า org.springframework.boot.autoconfigure.springbootapplication; นำเข้า org.springframework.boot.context.properties.enableConfigurationProperties; @springbootapplication แอปพลิเคชันคลาสสาธารณะ {โมฆะคงที่สาธารณะหลัก (สตริง [] args) {springapplication.run (application.class, args); -2. สร้างหน้าทดสอบ
สร้าง uploadform.html ใหม่ในไดเรกทอรีทรัพยากร/เทมเพลต
<html xmlns: th = "http://www.thymeleaf.org"> <body> <div th: if = "$ {message}"> <h2 th: text = "$ {message}"/> </div> อัปโหลด: </td> <td> <อินพุต type = "file" name = "file"/> </td> </tr> <tr> <td> </td> <td> <อินพุตประเภท = "ส่ง" value = "อัปโหลด"/> </td> </tr> Th: href = "$ {file}" rel = "ภายนอก nofollow" th: text = "$ {file}"/> </li> </ul> </div> </body> </html>3. สร้างบริการ Storageservice ใหม่
อินเทอร์เฟซ Storageservice
แพ็คเกจสวัสดีการจัดเก็บ; นำเข้า org.springframework.core.io.resource; นำเข้า org.springframework.web.multipart.multipartfile; นำเข้า java.nio.file.path; นำเข้า java.util.stream.stream; ส่วนต่อประสานสาธารณะ Storageservice {void init (); Void Store (ไฟล์ MultipartFile); สตรีม <Path> loadall (); โหลดพา ธ (ชื่อไฟล์สตริง); Resource LoadAsResource (ชื่อไฟล์สตริง); เป็นโมฆะ deleteAll (); -การใช้งาน Storageservice
แพ็คเกจสวัสดีการจัดเก็บ; นำเข้า org.springframework.core.io.resource; นำเข้า org.springframework.core.io.urlresource; นำเข้า org.springframework.stereotype.service; นำเข้า org.springframework.util.filesystemutils; นำเข้า org.springframework.util.stringutils; นำเข้า org.springframework.web.multipart.multipartfile; นำเข้า java.io.ioException; นำเข้า Java.net.Malformedurlexception; นำเข้า java.nio.file.files; นำเข้า java.nio.file.path; นำเข้า java.nio.file.Paths; นำเข้า java.nio.file.standardcopyoption; นำเข้า java.util.function.predicate; นำเข้า java.util.stream.stream; @Service คลาสสาธารณะ FILESYSTORAGESERVICE ใช้ StorageService {Private Final Path RootLocation = paths.get ("Upload-DIR"); / *** บันทึกไฟล์** @param ไฟล์ไฟล์*/ @Override โมฆะสาธารณะที่เก็บ (ไฟล์ MultipArtFile) {String filename = StringUtils.CleanPath (file.getOriginalFilename ()); ลอง {ถ้า (file.isempty ()) {โยน storageException ใหม่ ("ไม่สามารถจัดเก็บไฟล์ว่าง" + ชื่อไฟล์); } if (filename.contains (".. ")) {// นี่คือการตรวจสอบความปลอดภัยโยน storageException ใหม่ ("ไม่สามารถจัดเก็บไฟล์ที่มีเส้นทางสัมพัทธ์นอกไดเรกทอรีปัจจุบัน" + ชื่อไฟล์); } files.copy (file.getInputStream (), this.rootlocation.resolve (ชื่อไฟล์), StandardCopyOption.replace_existing); } catch (ioexception e) {โยน storageException ใหม่ ("ไม่สามารถจัดเก็บไฟล์" + ชื่อไฟล์, e); }} /*** แสดงรายการไฟล์ทั้งหมดด้านล่าง upload -dir* @return* /@Override สตรีมสาธารณะ <Ath> loadall () {ลอง {return files.walk (this.rootlocation, 1) // path ->! path.equals (rootlocation) - } catch (ioexception e) {โยน storageException ใหม่ ("ไม่สามารถอ่านไฟล์ที่เก็บไว้", e); }} @Override Public Path Load (ชื่อไฟล์สตริง) {return rootlocation.resolve (ชื่อไฟล์); } / *** รับทรัพยากรไฟล์* @param ชื่อไฟล์ชื่อไฟล์* @return Resource* / @Override ทรัพยากรสาธารณะ loadAsResource (ชื่อไฟล์สตริง) {ลอง {path file = โหลด (ชื่อไฟล์); ทรัพยากรทรัพยากร = urlresource ใหม่ (file.touri ()); if (resource.exists () || resource.isreadable ()) {return resource; } else {โยน storagefilenotfoundexception ใหม่ ("ไม่สามารถอ่านไฟล์:" + ชื่อไฟล์); }} catch (malformedurlexception e) {โยน storagefilenotfoundexception ใหม่ ("ไม่สามารถอ่านไฟล์:" + filename, e); }} / *** ลบไฟล์ทั้งหมดในไดเรกทอรี Upload-Dir* / @Override โมฆะสาธารณะ deleteAll () {filesystemutils.deleterecursively (rootlocation.tofile ()); } / *** การเริ่มต้น* / @Override โมฆะสาธารณะเริ่มต้น () {ลอง {files.createdirectories (rootlocation); } catch (ioexception e) {โยน storageException ใหม่ ("ไม่สามารถเริ่มต้นการจัดเก็บ", e); -StorageException.java
แพ็คเกจสวัสดีการจัดเก็บ; Public Class StorageException ขยาย RunTimeException {Public StorAgeException (ข้อความสตริง) {Super (ข้อความ); } public StorAgeException (ข้อความสตริง, สาเหตุที่น่าจดจำ) {super (ข้อความ, สาเหตุ); }} StorageFilEnotFoundException.javapackage สวัสดี. Storage; Public Class StorageFilenotFoundException ขยาย StorageException {Public StorageFilenotFoundException (ข้อความสตริง) {Super (ข้อความ); } public StorageFilenotFoundException (ข้อความสตริงสาเหตุที่ทำให้เกิดได้) {super (ข้อความ, สาเหตุ); -4. การสร้างคอนโทรลเลอร์
ใส่ไฟล์ที่อัปโหลดไว้ในไดเรกทอรีอัพโหลด DIR ของโครงการและแสดงรายการไฟล์ที่ดาวน์โหลดได้บนอินเทอร์เฟซตามค่าเริ่มต้น
ฟังก์ชั่น listuploadedfiles จะแสดงรายการไฟล์ทั้งหมดในไดเรกทอรีปัจจุบัน
ไฟล์ดาวน์โหลด servefile
ไฟล์อัปโหลด HandleFilePilePload
แพคเกจสวัสดี; นำเข้า java.io.ioException; นำเข้า java.util.stream.collectors; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.core.io.resource; นำเข้า org.springframework.http.httpheaders; นำเข้า org.springframework.http.responseentity; นำเข้า org.springframework.stereotype.controller; นำเข้า org.springframework.ui.model; นำเข้า org.springframework.web.bind.annotation.exceptionhandler; นำเข้า org.springframework.web.bind.annotation.getMapping; นำเข้า org.springframework.web.bind.annotation.pathvariable; นำเข้า org.springframework.web.bind.annotation.postmapping; นำเข้า org.springframework.web.bind.annotation.requestparam; นำเข้า org.springframework.web.bind.annotation.responsebody; นำเข้า org.springframework.web.multipart.multipartfile; นำเข้า org.springframework.web.servlet.mvc.method.annotation.mvcuricomponentsbuilder; นำเข้า org.springframework.web.servlet.mvc.support.redirectattributes; นำเข้า Hello.storage.storageFilenotFoundException; นำเข้าสวัสดี Storage.Storageservice; @Controller คลาสสาธารณะ FilePloadController {ส่วนตัว Storageservice StorageService ส่วนตัว @AutoWired Public FilePloadController (StorageService StorageService) {this.storageservice = StorageService; } @getMapping ("/") สตริงสาธารณะ listuploadedfiles (รุ่นโมเดล) พ่น IOException {model.addattribute ("ไฟล์", Storageservice.loadall (). แผนที่ (พา ธ -> mvcuricomponentsbuilder.fommethodname path.getFilename (). toString ()). build (). toString ()) .collect (collector.tolist ())); ส่งคืน "อัพโหลดฟอร์ม"; } @getMapping ("/files/{fileName:.+}") @ResponseBody การตอบสนองสาธารณะ <resource> ServeFile (@PathVariable String filename) {Resource File = StorageService.loadAsResource (ชื่อไฟล์); return responseEntity.ok (). ส่วนหัว (httpheaders.content_disposition, "ไฟล์แนบ; filename =/" " + file.getFileName () +" /"").body(file); } @PostMapping ("/") Public String HandleFileUpload (@RequestParam ("ไฟล์") ไฟล์ MultipArtFile, RedirectAtTributes RedirectAtTributes) {StorageService.store (ไฟล์); redirectattributes.addflashattribute ("ข้อความ", "คุณอัปโหลดสำเร็จ" + file.getoriginalfilename () + "!"); กลับ "เปลี่ยนเส้นทาง:/"; } @ExceptionHandler (StorageFilenotFoundException.class) ResponseEntity สาธารณะ <?> handlestorageFilenotfound (StorageFilenotFoundException exc) {return responseEntity.notfound (). สร้าง (); - ดาวน์โหลดซอร์สโค้ด: https://github.com/hellokittynii/springboot/tree/master/springbootuploadanddownload
สรุป
ด้านบนเป็นซอร์สโค้ดสำหรับการอัปโหลดและดาวน์โหลดไฟล์ Springboot ที่แนะนำโดยตัวแก้ไข ฉันหวังว่ามันจะเป็นประโยชน์กับคุณ หากคุณมีคำถามใด ๆ โปรดฝากข้อความถึงฉันและบรรณาธิการจะตอบกลับคุณทันเวลา ขอบคุณมากสำหรับการสนับสนุนเว็บไซต์ Wulin.com!