uploadify java实现多文件上传和预览
程序员文章站
2024-03-12 13:35:02
本文实例为大家分享了java文件上传和预览实现代码,供大家参考,具体内容如下
1、下载uploadify插件
2、index.html
本文实例为大家分享了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/uploadify-v3.1/uploadify.css" rel="stylesheet" type="text/css" /> <style type="text/css"> #uploader { position: relative; } #uploader_queue { position: absolute; width: 600px; left: 200px; top: 0; } </style> <script type="text/javascript"> $(function() { $("#file_upload") .uploadify( { 'auto' : false, 'method' : "get", 'formdata' : { 'folder' : 'file' }, 'height' : 30, 'swf' : '<@path/>/js/uploadify-v3.1/uploadify.swf', // flash 'uploader' : '<@path/>/uploadattach.do', // 'width' : 120, 'filetypedesc' : 'ֻ支持多种文件格式', 'filetypeexts' : '.dat;.264;.h264;.mp4;.dav;.mp4;.avi;.ts;.avi;' + '.mpg;.rmvb;.flv;.rm;.mov;.wmv;.jpg;.bmp;.png;.bmp;.jpg;.png;' + '.gif;.xlsx;.xls;.txt;.pdf;.doc;.docx;.rar;.zip;.7z', 'filesizelimit' : '800kb', 'buttontext' : '选择文件', 'uploadlimit' : 5, 'successtimeout' : 5, 'requeueerrors' : false, 'removetimeout' : 10, 'removecompleted' : false, 'queuesizelimit' : 10, 'queueid' : 'uploader_queue', 'progressdata' : 'speed', 'oninit' : function() { }, 'onuploadsuccess' : function(file, data, response) { $("#uploader_view").append( '<img height="60" alt="" src="<@path/>/upload/' + encodeuri(data) + '"/><br/><br/>'); }, 'onqueuecomplete' : function(queuedata) { $('#uploader_msg').html( queuedata.uploadssuccessful + '个文件上传成功<br/>'); } }); }); </script> </head> <body class=""> <@header/> <br /> <br /> <br /> <br /> <div id="uploader"> <p> <input type="file" name="file_upload" id="file_upload" /> </p> <a href="javascript:$('#file_upload').uploadify('upload','*')">上传</a> <a href="javascript:$('#file_upload').uploadify('stop')">取消上传</a> <div id="uploader_queue"></div> <div id="uploader_msg"></div> <div id="uploader_view"></div> </div> <br /> <br /> <br /> <br /> <@footer/> </body> </html>
3、java文件
package com.frame.core.ctrl; import java.io.file; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.printwriter; import java.util.date; import java.util.map; import java.util.uuid; import javax.servlet.servletexception; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.apache.log4j.logger; import org.springframework.stereotype.controller; import org.springframework.ui.modelmap; import 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", "笑傲江湖"); mav.addobject("projectname", "freemarker框架"); return mav; } @requestmapping(value = "/login") public void login(httpservletrequest request,httpservletresponse response) throws servletexception, ioexception { request.getsession().setattribute("username", "身份认证成功"); request.getrequestdispatcher("/index.jsp").forward(request, response); } @requestmapping("/uploadattach") public void processuploaddir(modelmap modelmap, multiparthttpservletrequest request, printwriter writer) throws exception { map<string, multipartfile> filemap = request.getfilemap(); string path = request.getsession().getservletcontext().getrealpath("/");; system.out.println("path:"+path); date currenttime = 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 = "is a illegal file"; throw new 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(); } finally { if (fileoutputstream != null) { try { fileoutputstream.close(); } catch (ioexception e) { e.printstacktrace(); } } } } writer.write(attachids.tostring().substring(0,attachids.tostring().length()-1)); } private final string[] filetype = new string[]{".dat",".264",".h264",".mp4",".dav",".mp4",".avi",".ts",".avi",".mpg",".rmvb",".flv",".rm",".mov",".wmv", ".jpg",".bmp",".png",".bmp",".jpg",".png",".gif", ".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; } } return false; } }
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。