uploadify文件上传插件
1 <div class="fl_left_box" style="float: left;"> 2 <span>选择上传文件:</span> <span> <input type="file" 3 id="uploadify" name="uploadify"></span> <span> 4 <div id="fileQueue" style="background-color: #FFFFFF;"></div> 5 </span> <span> <a href="javascript:void(0)" onclick="uploadFile();">开始上传</a> 6 </span> 7 </div>
js部分:
1 $("#uploadify").uploadify({ 2 'debug' : false, //开启调试 3 'auto' : false, //是否自动上传 4 'swf' : '${root}/js/uploadify-3.1/uploadify.swf', //引入uploadify.swf 5 'uploader' : 'sf_importData.action',//请求路径 6 'queueID' : 'fileQueue',//队列id,用来展示上传进度的 7 'width' : '70', //按钮宽度 8 'height' : '22', //按钮高度 9 'queueSizeLimit' : 1, //同时上传文件的个数 10 // 'fileTypeDesc' : '视频文件', //可选择文件类型说明 11 'fileTypeExts' : '*.*', //控制可上传文件的扩展名 12 'multi' : false, //允许多文件上传 13 'buttonText' : '选择文件',//按钮上的文字 14 'fileSizeLimit' : '9999999999', //设置单个文件大小限制 15 'fileObjName' : 'uploadify', //<input type="file"/>的name 16 'method' : 'post', 17 'removeCompleted' : true,//上传完成后自动删除队列 18 'onFallback' : function() { 19 alert("您未安装FLASH控件,无法导入!请安装FLASH控件后再试。"); 20 }, 21 'onUploadStart' : function(file) {//上传前执行 22 $("#uploadify").uploadify("settings", "formData", { 23 'type' : $('#type').val() 24 });//动态参数传递 25 }, 26 'onUploadSuccess' : function(file, data, response) {//单个文件上传成功触发 27 //data就是action中返回来的数据 28 }, 29 'onQueueComplete' : function() {//所有文件上传完成 30 alert("文件上传成功!"); 31 fnDraw($("#type").val()); 32 } 33 }); 34 tableReport('projectTable'); 35 });
后台:
1 private File uploadify;// 上传文件file对象 2 private String uploadifyFileName;// 上传文件名 (1.系统自动注入 2.变量命名有规则: 前台 3 // 对象名+"FileName") 4 private String uploadifyContentType;// 上传文件类型 (1.系统自动注入 2.变量命名有规 则: 5 // 前台对象名+"ContentType") 6 7 public String importData() { 8 // 得到保存上传文件的真实路径 9 String path = ServletActionContext.getServletContext().getRealPath("/upload"); 10 File dir = new File(path); 11 // 如果这个目录不存在,则创建它 12 if (!dir.exists()) { 13 dir.mkdir(); 14 } 15 BufferedOutputStream bos = null; 16 BufferedInputStream bis = null; 17 18 // 读取保存在临时目录下的上传文件,写入到新的文件中 19 try { 20 FileInputStream fis = new FileInputStream(uploadify); 21 bis = new BufferedInputStream(fis); 22 23 FileOutputStream fos = new FileOutputStream(new File(dir, uploadifyFileName)); 24 bos = new BufferedOutputStream(fos); 25 26 byte[] buf = new byte[4096]; 27 int len = -1; 28 while ((len = bis.read(buf)) != -1) { 29 bos.write(buf, 0, len); 30 } 31 Map<String, Object> map = new HashMap<String, Object>(); 32 map.put("filename", uploadifyFileName.trim()); 33 34 ServiceData vo = new ServiceData(); 35 36 // 文件上传时间 37 vo.setDatetime(CalendarUtils.formatDateTime(new Date())); 38 vo.setPath(uploadifyFileName); 39 vo.setFilename(uploadifyFileName); 40 vo.setType(type); 41 42 if (!serviceDataService.findByMap(map)) { 43 serviceDataService.save(vo); 44 } else { 45 serviceDataService.update(vo); 46 } 47 48 } catch (FileNotFoundException e) { 49 e.printStackTrace(); 50 } catch (IOException e) { 51 e.printStackTrace(); 52 } finally { 53 if (null != bis) { 54 try { 55 bis.close(); 56 } catch (IOException e) { 57 e.printStackTrace(); 58 } 59 } 60 if (null != bos) { 61 try { 62 bos.close(); 63 } catch (IOException e) { 64 e.printStackTrace(); 65 } 66 } 67 } 68 return null; 69 }