تقدم هذه المقالة وظائف التحميل والتنزيل في 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 application {public static void main (string [] args) {springapplication.run (application.class ، args) ؛ }}2. إنشاء صفحة اختبار
قم بإنشاء تحميل جديد. html في دليل الموارد/القوالب
<html xmlns: th = "http://www.thymeleaf.org"> <body> <div th: if = "$ {message}"> <h2 th: text = "$ {message}"/> </viv> <div> التحميل: </td> <td> <td type = "file" name = "file"/> </td> </td> <td> <td> </td> <td> <type type = "إرسال" value = "upload"/> </td> </tr> Th: href = "$ {file}" rel = "nofollow external" th: text = "$ {file}"/> </li> </ul> </viv> </body> </html>3. إنشاء خدمة storageservice جديدة
storageservice واجهة
حزمة hello.storage ؛ استيراد 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 () ؛ تحميل المسار (اسم ملف السلسلة) ؛ موارد loadasResource (اسم ملف السلسلة) ؛ void deleteall () ؛ }تنفيذ Storageservice
حزمة hello.storage ؛ استيراد 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 public class sethystorageservice تنفذ storageservice {private final path rootlocation = paths.get ("upload-dir") ؛ / *** حفظ الملف** param file file*/ Override public void Store (file multipartfile) {String filename = stringutils.cleanpath (file.getoriginalfilename ()) ؛ حاول {if (file.isempty ()) {رمي التخزين الجديد ("فشل في تخزين ملف فارغ" + اسم ملف) ؛ } if (filename.contains ("..")) {// هذا هو فحص أمني رمي تخزين جديد ("لا يمكن تخزين الملف مع المسار النسبي خارج الدليل الحالي" + اسم fileName) ؛ } files.copy (file.getInputStream () ، this.rootlocation.resolve (filename) ، standardcopyoption.replace_existing) ؛ } catch (ioException e) {رمي storageException جديد ("فشل في تخزين الملف" + اسم الملف ، e) ؛ } ستر }) ؛ } catch (ioException e) {رمي التخزين الجديد ("فشل في قراءة الملفات المخزنة" ، e) ؛ }} Override Public Path Load (اسم ملف السلسلة) {return rootlocation.Resolve (filename) ؛ } / *** احصل على موارد الملف* اسم ملف filename param* return Resource* / Override Public Resource LoadAsresource (اسم ملف السلسلة) {try {path file = load (filename) ؛ مورد المورد = urlresource الجديد (file.touri ()) ؛ if (Resource.exists () || Resource.isReadable ()) {return Resource ؛ } آخر {رمي storagefilenotfoundException ("لا يمكن قراءة الملف:" + filename) ؛ }} catch (malformedurlexception e) {رمي storagefilenotfoundException ("لا يمكن قراءة الملف:" + اسم الملف ، e) ؛ }} / *** حذف جميع الملفات في دليل التحميل diR* / Override public void deleteall () {fileystemutils.deleterecursively (rootlocation.tofile ()) ؛ } / *** initialization* / Override public void init () {try {files.createdirectories (rootlocation) ؛ } catch (ioException e) {رمي storageException جديد ("لا يمكن تهيئة التخزين" ، e) ؛ }}}StorageException.java
حزمة hello.storage ؛ يمتد StorageException الفئة العامة RunTimeException {public StorageException (رسالة سلسلة) {super (message) ؛ } StorageException (رسالة السلسلة ، سبب قابلية للتسمية) {super (message ، cause) ؛ }} storagefilenotfoundException.javapackage hello.storage ؛ الفئة العامة storageFilenotFoundException يمتد StorageException {storagefilenotfoundException (رسالة السلسلة) {super (message) ؛ } StorageFilenotFoundException (رسالة السلسلة ، السبب القابل للتسمية) {super (message ، cause) ؛ }}4. تحكم الخلق
ضع الملفات التي تم تحميلها في دليل تحميل المشروع ، وقم بإدراج الملفات القابلة للتنزيل على الواجهة افتراضيًا.
ستقوم وظيفة ListUploadedFiles بإدراج جميع الملفات في الدليل الحالي
خدمة تنزيل Servile
مقبض ملف التحميل
حزمة مرحبا استيراد 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.sponseentity ؛ استيراد 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 ؛ استيراد hello.storage.storageservice ؛ controller الفئة العامة fileUploAdController {private Final Storageservice Storageservice ؛ @autowired pileuploadController (Storageservice Storageservice) {this.storageservice = storageservice ؛ } getMapping ("/") السلسلة العامة listuploadedfiles (نموذج النموذج) يلقي ioException {model.addattribute ("files" ، storageservice.loadall (). map (path -> mvCuricomponentsBuilder.frommethodName (fileuploadcontroller.class ، path.getFilename (). toString ()). build (). إرجاع "تحميل" ؛ } getMapping ("/files/{filename:.+}") responsebody public reponseentity <srodust> servile (اسم ملف السلسلة pathvariable) {resource file = storageservice.loadasresource (filename) ؛ Return Responseentity.ok (). header (httpheaders.content_disposition ، "المرفق ؛ filename =/" " + file.getFilename () +" /"").body(file) ؛ } postmapping ("/") سلسلة public fallfileUpload ( @requestparam ("file") ملف multipartfile ، إعادة توجيه redirectattributes) {storageservice.store (file) ؛ redirectattributes.addflashattribute ("message" ، "تم تحميلك بنجاح" + file.getoriginalfilename () + "!") ؛ إرجاع "إعادة التوجيه:/" ؛ } exceptionHandler (storagefilenotfoundException.class) الاستجابة العامة <؟> handlestoragefilenotfound (StorageFilenotFoundException Exc) {return receptionentity.notfound (). build () ؛ }} رمز المصدر تنزيل: https://github.com/hellokittynii/springboot/tree/master/springbootuploadanddownload
لخص
ما سبق هو الرمز المصدري لتحميل وتنزيل ملفات Springboot التي قدمها لك المحرر. آمل أن يكون ذلك مفيدًا لك. إذا كان لديك أي أسئلة ، فيرجى ترك رسالة لي وسوف يرد المحرر إليك في الوقت المناسب. شكرا جزيلا لدعمكم لموقع wulin.com!