Uploadify jsp使用示例
uploadfiy这个插件是基于js里面的jquery库写的。结合了ajax和flash,实现了这个多线程上传的功能。
现在最新版为3.2.
下面里例子是3.1,部分上传代码使用了自己的封装好的类与方法,jsp中大致上使用方式差不多。
1.解压资源包,将以下文件拷贝到项目资源文件夹里。
![](http://images0.cnblogs.com/blog2015/305369/201503/062041291338816.jpg)
2.表单的引用(更多的参数:查看)
<!--Jquery--> <script type="text/javascript" src="linkey/bpm/easyui/jquery.min.js"></script> <!--FileUpload--> <link rel="stylesheet" href="linkey/oa/uploadify/uploadify.css" type="text/css"></link> <script type="text/javascript" src="linkey/oa/uploadify/jquery.uploadify-3.1.min.js"></script> <script> $(function() { //加载上传附件控件 $("#uploadify").uploadify({ 'height' : 18, 'width' : 60, 'buttonText' : '浏 览', 'swf' : 'linkey/oa/uploadify/uploadify.swf', 'uploader' : 'rule?wf_num=R_Mail_B002&DocUnid='+WF_DocUnid, 'auto' : true, 'onUploadStart' : function(file){ //开始上传.. mask(); }, 'onUploadSuccess':function(file) { //上传成功后处理 unmask(); } }); }); </script> <body> <table> <tr> <td style="word-break: break-all;" valign="middle"> 附件 </td> <td valign="middle"> <div id="fileQueue"></div> <p> <input id="uploadify" name="uploadify" type="file"/> </p> </td> </tr> </table> </body>
3.上传处理程序
package cn.linkey.rulelib.Mail; import java.util.*;import java.io.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.*; import org.apache.commons.fileupload.disk.*; import org.apache.commons.fileupload.servlet.*; /** * @RuleName:上传邮件附件 * @author admin * @version: 8.0 */ final public class R_Mail_B002 { @Override public String run(HashMap<String, Object> params) throws Exception { //params为运行本规则时所传入的参数 HttpServletRequest request = BeanCtx.getRequest(); String DocUnid = BeanCtx.g("DocUnid"); String name = ""; //随机文件名称 String fileName = ""; //文件原始名称 String extName = ""; //扩展名 //上传文件目录(以月份+Userid为目录) String PATH = "/uploads/"+DateUtil.getNow("yyyyMM")+"/"+BeanCtx.getUserid()+"/"; String savePath = request.getServletContext().getRealPath(""); //this.getServletConfig().getServletContext().getRealPath(""); savePath = savePath + PATH; File f1 = new File(savePath); if (!f1.exists()) { f1.mkdirs(); } DiskFileItemFactory fac = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(fac); upload.setHeaderEncoding("utf-8"); List fileList = null; try { fileList = upload.parseRequest(request); } catch (FileUploadException ex) { return ""; } Iterator<FileItem> it = fileList.iterator(); while (it.hasNext()) { FileItem item = it.next(); if (!item.isFormField()) { name = item.getName(); fileName = name; String sizeUnit = "KB"; long size = 0; if(item.getSize()>1024*1024*1024){ size = item.getSize()/1024/1024/1024; sizeUnit = "GB"; }else if(item.getSize()>1024*1024){ size = item.getSize()/1024/1024; sizeUnit = "MB"; }else{ size = item.getSize()/1024; } String type = item.getContentType(); if (name == null || name.trim().equals("")) { continue; } // 扩展名格式: if (name.lastIndexOf(".") >= 0) { extName = name.substring(name.lastIndexOf(".")); } File file = null; do { // 生成文件名: name = UUID.randomUUID().toString(); file = new File(savePath + name + extName); } while (file.exists()); File saveFile = new File(savePath + name + extName); try { item.write(saveFile); SaveFileInfo(DocUnid,PATH,fileName,size+sizeUnit,name+extName); } catch (Exception e) { e.printStackTrace(); } } } return ""; } //保存附件信息到邮箱附件表 public void SaveFileInfo(String docUnid,String filePath,String fileName,String fileSize,String fileRandomUUID){ Document nDoc = new Document("Mail_Attachments"); nDoc.s("DocUnid",docUnid); nDoc.s("FilePath",filePath); nDoc.s("FileName",fileName); nDoc.s("FileSize",fileSize); nDoc.s("FileRandomUUID",fileRandomUUID); nDoc.save(); } }