ダウンロード:
1.Spring-MVCで構成(100m未満のファイルをダウンロードするため)
<bean> <プロパティ名= "messageconverters"> <list> <! - configure download return type-> <bean/> <bean> <! - configure encoding method-> <property name = "supportedmediatypes" value = "application/json; charset = utf-8"/> </bean>
ファイルコードをダウンロードします
@RequestMapping( "/file/{name.rp}")public Reponsementity <byte []> filedownload( @pathvariable( "name.rp")string name、httpservletrequents request、httpservletresponse response){// @pathvariable文字System.out.println( "<name>"+name); // system.out.println( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"] CSS、JS、JSON、GIF、PNG、BMP、JPG、ICO、DOC、DOC、DOC、DOCX、XLSX、TXT、SWF、PDF*** ///静的積み込み干渉感触F = new feelutile(); name = f.getfileformat(name); String Pathstring = "C:C:// file file = new file(pathstring); httpheaders headers = new httpheaders(); // string filename = urlencoder.encode(name、 "utf-8"); // by = fileutils.readfiletobytearray(file); headers.setContentType(mediatype.application_octet_stream); // urlencoder.encode(filename、 "utf-8")headers.setContentDispositionFormData( "Attachment"、fileName); ResponseTentity <byte []>(by、headers、httpstatus.created);} catch(Exception e){e.printstacktrace(); try {request.getRequestDispatcher( "/error/404.jsp")。 blocke1.printstacktrace();} catch(ioexception e1){// todo auto-feenated catchblocke1.printstacktrace();}} return re;}ファイルのアップロード:
Spring-MVCの1つの構成
<! - 4。ファイルアップロード構成ファイルアップロード - > <bean id = "multipartresolver" class = "org.springframework.web.multipart.commons.commonsmultipartresolver"> <プロパティ名= "defaultencoding"> <balue> utf-8 </value> </property> <Property name = "maxuploadsize"> <value> 1048576000 </value> </property> <property name = "maxinmemorysize"> <value> 40960 </value> </property> </bean>
コントローラーのコードは次のとおりです
@RequestMapping(value = "/upload"、method = requestmethod.post)@responsebodypublic json upload(doc doc、 @requestparam( "uploadfile")commonsmultipartfile file){json j = new json(); try {string realpath = this servletcontext.getRealpath "); string uploadfilefilename = file.getoriginalFileName(); string uploadFileFileNameWithSpace = uploadFileFileName.ReplaceAll( "" "、" "); string filetype = uploadfilefilenamewithoutspace.substring(uploadfilefilenamewithoutspace.lastindexof( "。")); file targetfile = new file(realpath+file.separator、uploadfilefilenamewithoutspace); if(targetfile.exists()){targetfile.delete();} file.getFileItem()。write(targetfile); docservice.upload(doc、uploadfilefilenamewithspace); j.setsuccess(true); j.setmsg( "upload Manual rackerfully");} catch(exgger.error(excepdutil.getexceptionmessage(e)); j.setmsg( "upload unsuccessfly");上記は、編集者によって導入されたJava Spring MVCアップロードおよびダウンロードファイル構成およびコントローラーメソッドの詳細な説明です。私はそれが誰にでも役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は、すべての人に時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!