Contoh -contoh dalam artikel ini berbagi unggahan file java dan kode implementasi pratinjau untuk referensi Anda. Konten spesifiknya adalah sebagai berikut
1. Unduh plugin unggah
2. Index.html
<! Doctype html> <html lang = "en"> <ead> <@head/> <script src = "<@path/>/js/unggah-v3.1/jQuery.uploadify-3.1.js"> </script> <link href = "<@path // js/unggahan-v3.1 type = "text/css"/> <style type = "text/css"> #uploader {position: relatif; } #uploader_queue {position: absolute; Lebar: 600px; Kiri: 200px; TOP: 0; } </tyle> <type skrip = "text/javascript"> $ (function () {$ ("#file_upload") .uploadify ({'auto': false, 'Metode': "get", 'formData': {'path/' unggah/}}, 'unggah': 'swf':@path/@path/@path/@path/@path/@ flash 'unggah': '<@path // uploadattach.do', // 'width': 120, 'filetypedesc': 'ֻ mendukung beberapa format file', 'filetypeExts': '.dat; .264; .h264; .mp4; .dav; .mp4; . '800KB', 'buttonText' : 'select file', 'uploadLimit' : 5, 'successTimeout' : 5, 'requeueErrors' : false, 'removeTimeout' : 10, 'removeCompleted' : false, 'queueSizeLimit' : 10, 'queueID' : 'uploader_queue', 'progressData' : 'speed', 'onInit' : function() { }, 'onuploadSucCess': function (file, data, respons) {$ ("#unggah_view"). append ('<img src = "<@path /> /unggah /' + encodeuri (data) + '" /> <br /> <br /> <br /> <br /<br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br }, 'onqueueComplete': function (queuedata) {$ ('#unggah_msg'). html (queuedata.uploadsuccessful + 'diunggah dengan sukses <br/>'); }}); }); </script> </head> <body> <@header /> <br /> <br /> <br /> <br /> <div id = "unggah"> <p> <input type = "file" name = "file_upload" id = "file_upload" /> </p> <a href = "JavaScript: $ ('#file_upload'). Unggok ('Unggah', '*')"> Unggah </a> <a href = "javascript: $ ('#file_upload'). Divloady ('stop')"> unggah </a> <div id = "unggah_que"> div </dive </a> </a> </a> </ve id = "unggah_que"> "> Dive-"> unggahan </a> </a> </e> </veD = "unggah"> "> Diveue"> unggah </a> </a> </a> </a> </a> </a> </a> </a> </a> </a> </a> </a> </a> </a> </a> </a> </a> </a> </a> <Div id = "unggah_view"> </div> </div> <br/> <br/> <br/> <br/> <@footer/> </body> </html> 3. File Java
paket com.frame.core.ctrl; impor java.io.file; impor java.io.filenotfoundException; impor java.io.fileoutputStream; impor java.io.ioException; impor java.io.printwriter; impor java.util.date; impor java.util.map; impor java.util.uuid; impor javax.servlet.servletException; impor javax.servlet.http.httpservletRequest; impor javax.servlet.http.httpservletResponse; impor org.apache.log4j.logger; impor org.springframework.stereotype.controller; impor org.springframework.ui.modelmap; impor org.springframework.web.bind.annotation.requestmapping; impor org.springframework.web.multipart.multipartfile; impor org.springframework.web.multipart.multipartttpservletRequest; impor org.springframework.web.servlet.modelandview; @Controller kelas publik loginctrl {private static logger log = logger.getLogger (loginctrl.class); @RequestMapping (value = "/goIndex") public ModelAndView goIndex () {ModelAndView mav = new ModelAndView ("index"); mav.addObject ("name", "pedang"); mav.addObject ("ProjectName", "Freemarker Framework"); mengembalikan mav; } @RequestMapping (value = "/login") Login public void (permintaan httpservletRequest, httpservletResponse response) melempar servletException, ioException {request.getSession (). SetAttribute ("USERNAME", "Authentity Authentity gugar"); request.getRequestDispatcher ("/index.jsp"). Forward (Request, Response); } @RequestMapping ("/unggahAttach") public void ProcessUploadDir (ModelMap ModelMap, MultipArtHTTPServletRequest Request, Printwriter Writer) melempar Exception {MAP <String, MultipartFile> FileMap = request.getFileMap (); String path = request.getSession (). GetSerVletContext (). GetRealPath ("/") ;; System.out.println ("Path:"+Path); Tanggal saat ini = tanggal baru (); awalan panjang = currentTime.getTime (); StringBuffer attachIds = new StringBuffer (); untuk (map.entry <string, multipartfile> f: filemap.entryset ()) {multipartFile file = f.getValue (); if (! isLegalfile (file)) {string msg = "adalah file ilegal"; Lempar RuntimeException baru (MSG); } String originalFileName = prefix + "_" + file.getoriginalFileName (); File FileDir = File baru (path + "/unggah" + file.separator); if (! FileDir.exists ()) {filedir.mkdirs (); } File file = file baru (path + "/unggah" + file.separator + originalFileName); FileOutputStream FileOutputStream = null; coba {fileoutputStream = new FileOutputStream (file); fileOutputStream.write (file.getBytes ()); fileOutputStream.flush (); lampiran.append (originalFileName + ","); } catch (FileNotFoundException e) {e.printstacktrace (); } catch (ioException e) {e.printstacktrace (); } catch (Exception e) {E.PrintStackTrace (); } akhirnya {if (fileoutputStream! = null) {coba {fileoutputStream.close (); } catch (ioException e) {e.printstacktrace (); }}}} writer.write (lampiran.toString (). Substring (0, attachids.toString (). Length ()-1)); } private final string [] filetype = new String [] {". ".rmvb", ". flv", ". ".xlsx", ". xls", ". txt", ". Pdf", ". Doc", ". Docx", ".rar", ". Zip", ". 7z"}; private boolean islegalfile (file multipartfile) {string igherFileName = file.getoriginalfileName (); untuk (string ft: filetype) {if (originalFileName.endswith (ft)) {return true; }} return false; }}Gambar reproduksi:
Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.