تشترك هذه المقالة في الرمز المحدد لـ Spring MVC لتنفيذ وظائف تحميل الملف وتنزيلها للرجوع إليها. المحتوى المحدد كما يلي
تحميل الملف
قدم الجرار الربيع MVC و Commons-FilePload في pom.xml
<!-Spring MVC-> <reperency> <roupiD> org.springframework </rougiD> <StifactId> Spring-Webmvc </shintifactid> <الإصدار> 4.3.13 <StifactId> commons-filepload </artifactId> <soph> 1.3.3 </version> </reperence>
إضافة تكوين تحميل الملفات في springmvc.xml
<bean id = "multipartresolver"> <!-تحميل حجم الملف العليا ، الوحدة في البايت (10 ميجابايت)-> <property name = "maxuploadsize"> <suality> 10485760 </value> </property> <! الافتراضي هو ISO-8859-1-> <property name = "defaultencoding"> <value> utf-8 </value> </property> </ban>
إضافة نموذج النموذج إلى ملف JSP
<form action = "expload" enctype = "multipart/form-data" method = "post"> <table> <tr> <td> وصف الملف: </td> <td> <type type = "text" <td> <input type = "submit" value = "upload"> </td> </tr> </table> </pump>
كيفية إضافة تحميل الملف
// قم بتحميل الملف سيتم ربط الملف تلقائيًا بـ multipartfile @requestmapping (value = "/upload" ، method = requestMethod.post) تحميل السلسلة العامة (httpservletrequest طلب @requestparam ("الوصف") الوصف ، if (! file.isempty ()) {// قم بتحميل مسار سلسلة الملف = request.getServletContext (). getRealPath ("/file/") ؛ // تحميل اسم fileName filename = file.getoriginalfilename () ؛ File FilePath = ملف جديد (المسار ، اسم الملف) ؛ // احكم على ما إذا كان المسار موجودًا ، وإذا لم يكن موجودًا ، فقم بإنشاء If (! filePath.getParentFile (). } // احفظ ملف التحميل إلى ملف ملف الهدف. إرجاع "النجاح" ؛ } آخر {return "error" ؛ }}ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.