この記事の例は、参照用のJavaファイルのアップロードおよびプレビュー実装コードを共有しています。特定のコンテンツは次のとおりです
1. uploadifyプラグインをダウンロードします
2。index.html
<!doctype html> <html lang = "en"> <head> <@head/> <script src = "<@path/>/js/uploadify-v3.1/jquery.uploadify-3.1.js"> </script> <link href = "<@path // js type = "text/css"/> <style type = "text/css"> #uploader {position:relative; } #uploader_queue {position:absolute;幅:600px;左:200px;上:0; } </style> <script type = "text/javascript"> $(function(){$( "#file_upload").uploadify({'auto':false、 'method': "get"、 'formdata':{'folder': 'file'}、 'height':30、 'swsf': '" // flash 'uploader': '<@path // uploadattach.do'、// 'width':120、 'filetypedesc': 'サポート' 'filetypeexts': '.dat; .264; .h264; .mp4; .dav; .avi; .avi; .avi; .avi; .avi; '.mpg; rmvb; .flv; .rm; .mov; .wmv; .jpg; .bmp; .png; .bmp; .jpg; .png;' .gif; .xlsx; .xls; .txt; .pdf; .doc; .docx; .docx; .docx; .docx; .docx; .docx; .docx; .docx; .docx; .docx; .docx; .docx; '800kb'、 'buttontext': 'select file'、 'uploadlimit':5、 'successtimeout':5、 'queueerrors':false、 'removetimeout':10、 'remocompleted':false、 'queueizelimit':10、queueid ':' uploader_queue '、' uploader_queid '、 }、 'onuploadsuccess':function(file、data、response){$( "#uploader_view")。 }、 'onqueuecomplete':function(queuedata){$( '#uploader_msg')。html(queuedata.uploadssucessful + 'アップロード<br/>'); }}); }); < /script> < /head> <body> <@header /> <br /> <br /> <br /> <br /> <div id = "uploader"> <p> <入力タイプ= "ファイル" name = "file_upload" id = "file_upload" /> < /p> <a href = "javascript:$( '#file_upload')。uploadify( 'upload'、 '*')"> upload </a> <a href = "javascript:$( '#file_upload') id = "uploader_msg"> </div> <div id = "uploader_view"> </div> </div> <br/> <br/> <br/> <@footer/> </body> </html> </html> < 3。Javaファイル
パッケージcom.frame.core.ctrl; java.io.fileをインポートします。 java.io.filenotfoundexceptionをインポートします。 java.io.fileoutputStreamをインポートします。 java.io.ioexceptionをインポートします。 java.io.printwriterをインポートします。 Import Java.util.date; java.util.mapをインポートします。 java.util.uuidをインポートします。 javax.servlet.servletexceptionをインポートします。 javax.servlet.http.httpservletrequestをインポートします。 javax.servlet.http.httpservletResponseをインポートします。 org.apache.log4j.loggerをインポートします。 org.springframework.stereotype.controllerをインポートします。 Import org.springframework.ui.modelmap; org.springframework.web.bind.annotation.requestmappingをインポートします。 Import org.springframework.web.multipart.multipartfile; Import org.springframework.web.multipart.multiparthttpservletrequest; Import org.springframework.web.servlet.modelandview; @controller public class loginctrl {private static logger log = logger.getLogger(loginctrl.class); @RequestMapping(value = "/goindex")public modelandview goindex(){modelandview mav = new modelandview( "index"); mav.addobject( "name"、 "Swordsman"); mav.addobject( "projectname"、 "freemarkerフレームワーク"); mavを返します。 } @RequestMapping(value = "/login")public void login(httpservletrequest request、httpservletresponse応答)スローServletexception、ioexception {request.getSession()。 request.getRequestDispatcher( "/index.jsp")。 } @RequestMapping( "/uploadAttach")public void processuploaddir(modelmap modelmap、multiparthttpservletrequest request、printwriter writer)スロー{map <string、multipartfile> filemap = request.getFileMap(); string path = request.getSession()。getServletContext()。getRealPath( "/");; System.out.println( "path:"+path);日付curnertime = new date(); long prefix = currentTime.getTime(); stringbuffer attachIds = new StringBuffer(); for(map.entry <string、multipartfile> f:filemap.entryset()){multipartfile file = f.getValue(); if(!islegalfile(file)){string msg = "は違法ファイルです";新しいruntimeexception(msg)を投げる; } string originalfileName = prefix + "_" + file.getoriginalFileName(); file filedir = new file(path + "/upload" + file.separator); if(!filedir.exists()){filedir.mkdirs(); } file files = new file(path + "/upload" + file.separator + originalFileName); fileoutputStream fileoutputStream = null; try {fileoutputStream = new fileoutputStream(files); fileoutputStream.write(file.getBytes()); fileoutputStream.flush(); attachids.append(originalfilename + "、"); } catch(filenotfoundexception e){e.printstacktrace(); } catch(ioexception e){e.printstacktrace(); } catch(Exception e){e.printstacktrace(); }最後に{if(fileoutputStream!= null){try {fileoutputStream.close(); } catch(ioexception e){e.printstacktrace(); }}}} writer.write(actitionIds.toString()。substring(0、attachIds.toString()。length() - 1)); } private final string [] filetype = new string [] {"。dat"、 "。264"、 "。H264"、 "。MP4"、 " ".rmvb"、 "。flv"、 "。rm"、 "。mov"、。wmv "、。jpg"、 "。bmp"、 "。png"、 "。bmp"、 "。Jpg"、。 ".xlsx"、 "。xls"、 "。txt"、 "。pdf"、 "。doc"、 "。docx"、 ".rar"、 "。zip"、 "。7z"}; private boolean islegalfile(multipartfile file){string originalfilename = file.getoriginalfilename(); for(string ft:filetype){if(originalfilename.endswith(ft)){return true; }} falseを返します。 }}複製画像:
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。