Unduh:
1. Konfigurasi di Spring-MVC (untuk mengunduh file di bawah 100m)
<Bean> <name properti = "MessageConVerters"> <cist> <!-Mengkonfigurasi Tipe Pengembalian Unduh-> <bean/> <bean> <!-Mengkonfigurasi metode pengkodean-> <nama properti = "SupportedMediatypes" value = "Application/JSON; Charset = UTF-8"/</bean> </list> </list> </json; charset = UTF-8 "/</bean> </list> </list> </propert
Unduh kode file
@RequestMapping ("/file/{name.rp}") Tanggapan publik <byte []> fileDownload (@pathvariable ("name.rp") nama string, httpservletRequest, httpservletResponse) {// @pathvariable name, // @rexparse name) {// @pathvariable name, // @ReFLEFLEXPONSE) {// @pathvariable name, // @ReFLEFLEQUESTSE) {// @pathvariable name, // @ReFletponse) System.out.println ("<name>"+name); // System.out.println (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); responEnentity <byte []> re = null; CSS, JS, JSON, GIF, PNG, BMP, JPG, ICO, DOC, DOC, DOCX, XLSX, TXT, SWF, PDF*** /// Unduh untuk mencegah interferensi pemuatan statis Feelutile f = new feelutile (); name = f.getfileformat (name); string Pathstring = "nenel (); name = f.getFileformat (name); string Pathstring =" nenel (); name = f.getFileformat (name); string Pathstring = " File File = File Baru (PathString); header httpheaders = httpheaders baru (); // string filename = urlencoder.encode (nama, "utf-8"); // Untuk menyelesaikan masalah nama Chinese string yang dikocok = new string (name.getBytes ("UTF-8"), "" oleh = Fileutils.readFiletObyTeArray (file); headers.setContentType (mediatype.application_octet_stream); // urlencoder.encode (fileename, "utf-8") headers.setContentDispositionFormData ("lampiran", file ResponEntity <byte []> (oleh, header, httpstatus.created);} catch (exception e) {e.printstacktrace (); coba {request.getRequestDispatcher ("/error/404.jsp"). Maju, respons, respons);} catch (servletException e1) o1) {{{{{response); blocke1.printstacktrace ();} catch (ioException e1) {// todo Auto-generated tangkap blocke1.printstacktrace ();}} return re;}File Unggah:
1 konfigurasi di Spring-MVC
<!-4. Unggah file konfigurasi unggah file-> <bean id = "multipartresolver" class = "org.springframework.web.multipart.commons.commonsmultipartresolver"> <Properti Nama = "DefaultEncoding"> <value> UTF-8 </value> </properti> Properti> Properti> Properten> name = "MAXUPLOADSIZE"> <value> 1048576000 </ value> </propt> <name properti = "maxInmemorySize"> <value> 40960 </ value> </prop Property> </bean>
Kode dalam pengontrol adalah sebagai berikut
@RequestMapping (value = "/unggah", metode = requestMethod.post) @ResponseBodyPublic JSON unggah (Doc Doc, @RequestParam ("unggahfile") commonsMultipartFile file) {json j = new json (); coba {string realpath = this.servletex. String unggahFileFileName = file.getoriginalFileName (); String unggahfileFileNameWithoutSpace = unggahfileFileName.replaceall ("", ""); String filetype = unggahFileFileNeMeWithoutSpace.substring (unggahFileFileNeMeWithoutSpace.LastIndexOf (".")); File targetFile = file baru (realpath+file.separator, unggahfileFileFileNamewithoutSpace); if (targetFile.exists ()) {targetfile.delete ();} file.getFileItem (). tulis (targetFile); docservice.upload (doc, unggahfileFileNameWithoutSpace); j.setsuccess (true); j.setmsg ("unggah manual berhasil");} catch (pengecualian e) {logger.error (exceptionutil.getExceptionMessage (e)); j.setmsg ("unggah manual yang lebih baik (" unggah ("unggah manual (" unggah ("unggah (e); j.setmsg (" unggah ("unggah manual unggah (" unggah ("unggah (" unggah ("unggah (" unggah ("unggah (" unggah (e)); j.setMSG ("unggah (" unggah ("unggah (" unggah manual manual unggah ("Di atas adalah penjelasan terperinci tentang unggahan Java Spring MVC dan unduhan konfigurasi file dan metode pengontrol yang diperkenalkan oleh editor. Saya harap ini akan membantu semua orang. Jika Anda memiliki pertanyaan, silakan tinggalkan saya pesan dan editor akan membalas semua orang tepat waktu. Terima kasih banyak atas dukungan Anda ke situs web Wulin.com!