herunterladen:
1. Konfigurieren Sie in Spring-MVC (zum Herunterladen von Dateien unter 100 m)
<bean> <Eigenschaft name = "MessageConverters"> <List> <!-Download-Rückgabe-Typ konfigurieren-> <bean/> <bean> <!-Konfigurieren Sie die Codierungsmethode-> <Eigenschaft name = "SupportedMediTypes" value = "application/json; charset = utf-8"/> </bean> </list> </banes.
Dateicode herunterladen
@RequestMapping ("/Datei/{name.rp}") public responseentity <byte []> fileDownload (@PathVariable ("name.rp") String -Name, httpServletRequest -Anforderung, httpServletRetRePletRepuls) {//pathvariable String String -Name, // @RequestParam ("name"). System.out.println ("<name>"+name); // system.out.println (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>; CSS, JS, JSON, GIF, PNG, BMP, JPG, ICO, DOC, DOC, DOCX, XLSX, TXT, SWF, PDF*** /// Download, um statische Lade -Störungen Feelutile F = New Feelutile (); name = f.getFileformat (name) zu verhindern; Datei Datei = neue Datei (Pathstring); von = FileUtils.ReadFiletObytearray (Datei); Header.SetContentType (minyType.Application_octet_stream); // urlencoder.encode (fileName, "utf-8") Headers.setContentDispositionFositionFositionData; Antwortabteilung <Byte []> (von Headern, httpstatus.Created);} catch (Ausnahme E) {E.printstacktrace (); try {request.getRequestDispatcher ("/error/404.jsp"). blocke1.printstacktrace ();} catch (ioException e1) {// Todo automatisch generatter Catch Blocke1.printstacktrace ();}} return Re;}Datei hochladen:
1 Konfiguration im Spring-MVC
<!-4. Datei-Upload-Konfigurationsdatei Upload-> <bean id = "multipartresolver" class = "org.springFramework.web.multiPart.Commons.CommonsMultipArtresolver"> <Eigenschaft name = "DefaultEnCoding" name = "maxUploadSize"> <wert> 1048576000 </value> </property> <Eigenschaft name = "maxinMemorySize"> <wert> 40960 </value> </property> </bean>
Der Code im Controller ist wie folgt
@RequestMapping (value = "/upload", method = requestMethod.post) @ResponseBodypublic JSON Upload (doc doc, @RequestParam ("UploadFile") commonsMultipartFile -Datei {json j = new json (); try {String Realpath = this.ServeTFile. String uploadFileFileName = Datei.getOriginalFileName (); String uploadFileFileNameWitHoutSpace = UploadFileFileName.replaceall ("", ""); String fileType = uploadFileFileNameWitHoutSpace.substring (UploadFileFileNameWitHoutSpace.lastIndexof (".")); Datei targetFile = new Datei (RealPath+Datei.separator, UploadFileFileNameWithoutSpace); if (targetFile.exists ()) {targetFile.delete ();} file.getFileItem (). Write (targetFile); docService.Upload (doc, uploadFileFileName ohne Space);Das obige ist eine detaillierte Erläuterung des vom Editors vorgestellten Java Spring MVC -Upload- und Download -Dateikonfiguration und -vertragsmethode. Ich hoffe, es wird für alle hilfreich sein. Wenn Sie Fragen haben, hinterlassen Sie mir bitte eine Nachricht und der Editor wird allen rechtzeitig antworten. Vielen Dank für Ihre Unterstützung auf der Wulin.com -Website!