JQuery上传插件Uploadify并传参数

3 篇文章 0 订阅

也可以去看看官网上面的 dome  下载包...API  等

 

下面是我开发过程遇到的一些问题总结:

 1、上传失败 IO ERROR    ------测试是否是 servlet 等配置或者关注flash的版本

 2、前台传参中文乱码  -----------这个要根据应用服务器不同可能不同吧...反正只要我们的 界面、界面传参以及后台接收的编码设置一致应该就没上面问题..反正这个问题好解决的...

 3、批量上传的时候,只有第一个附件上传的时候可以接收到前台传来的name参数,而后面的参数都为null-------设置'simUploadLimit' : 1, // 一次同步上传的文件数目为 1,问题解决...当初这个问题可是难了我好久!fuck

 

嘿嘿....下面我贴出代码 

 

 jsp:

 

Html代码   收藏代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>  
  2. <%  
  3.     String path = request.getContextPath();   
  4. %>  
  5.   
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  7. <html>  
  8.     <head>   
  9.         <title>QQ:609865047  ---  妞见妞爱</title>  
  10.         <meta http-equiv="pragma" content="no-cache">  
  11.         <meta http-equiv="cache-control" content="no-cache">  
  12.         <meta http-equiv="expires" content="0">  
  13.     </head>  
  14.   
  15.     <body>   
  16.         <link href="js/uploadify.css" rel="stylesheet" type="text/css" />  
  17.         <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>  
  18.         <script type="text/javascript" src="js/swfobject.js"></script>  
  19.         <script type="text/javascript" src="js/jquery.uploadify.v2.1.0.min.js"></script>  
  20.         <script type="text/javascript">  
  21.          $(document).ready(function() {  
  22.           $("#uploadify").uploadify({  
  23.            'uploader'       : 'js/uploadify.swf',  
  24.            'script'         : 'scripts/uploadify',//servlet的路径或者.jsp 这是访问servlet 'scripts/uploadif'   
  25.            'method'         :'GET',  //如果要传参数,就必须改为GET  
  26.            'cancelImg'      : 'js/cancel.png',  
  27.            'folder'         : 'uploads', //要上传到的服务器路径,  
  28.            'queueID'        : 'fileQueue',  
  29.            'auto'           : false, //选定文件后是否自动上传,默认false  
  30.            'multi'          : true, //是否允许同时上传多文件,默认false  
  31.            'simUploadLimit' : 1, //一次同步上传的文件数目    
  32.            'sizeLimit'      : 19871202, //设置单个文件大小限制,单位为byte    
  33.            'queueSizeLimit' : 5, //限制在一次队列中的次数(可选定几个文件)。默认值= 999,而一次可传几个文件有 simUploadLimit属性决定。  
  34.            'fileDesc'       : '支持格式:jpg或gif', //如果配置了以下的'fileExt'属性,那么这个属性是必须的    
  35.            'fileExt'        : '*.jpg;*.gif',//允许的格式  
  36.            'scriptData'     :{'name':$('#name').val()}, // 多个参数用逗号隔开 'name':$('#name').val(),'num':$('#num').val(),'ttl':$('#ttl').val()  
  37.             onComplete: function (event, queueID, fileObj, response, data) {  
  38.             var value = response ;  
  39.                alert("文件:" + fileObj.name + "上传成功");  
  40.             },    
  41.             onError: function(event, queueID, fileObj) {    
  42.              alert("文件:" + fileObj.name + "上传失败");    
  43.             },    
  44.             onCancel: function(event, queueID, fileObj){    
  45.              alert("取消了" + fileObj.name);    
  46.               }   
  47.           });  
  48.          });  
  49.            
  50.            
  51.          function uploasFile(){   
  52.               //校验  
  53.               var name=document.getElementById("name").value;   
  54.               if(name.replace(/\s/g,'') == ''){  
  55.                     alert("名称不能为空!");  
  56.                     return false;  
  57.               }    
  58.               //设置 scriptData 的参数  
  59.               $('#uploadify').uploadifySettings('scriptData',{'name':$('#name').val()});  
  60.               //上传  
  61.               jQuery('#uploadify').uploadifyUpload()               
  62.          }  
  63.             
  64.            
  65.  </script>  
  66.          名称:<input type="text" id="name" name="name" value="妞见妞爱" >  
  67.         <div id="fileQueue"></div>   
  68.         <input type="file" name="uploadify" id="uploadify" />  
  69.         <p>  
  70.             <a href="javascript:uploasFile()">开始上传</a>&nbsp;  
  71.             <a href="javascript:jQuery('#uploadify').uploadifyClearQueue()">取消所有上传</a>  
  72.         </p>  
  73.     </body>  
  74. </html>  

  

 

 

 Uploadify.java

 

  

Java代码   收藏代码
  1. package com;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.text.SimpleDateFormat;  
  9. import java.util.Date;  
  10. import java.util.Iterator;  
  11. import java.util.List;  
  12.   
  13. import javax.servlet.ServletException;  
  14. import javax.servlet.http.HttpServlet;  
  15. import javax.servlet.http.HttpServletRequest;  
  16. import javax.servlet.http.HttpServletResponse;  
  17.   
  18. import org.apache.commons.fileupload.FileItem;  
  19. import org.apache.commons.fileupload.FileUploadException;  
  20. import org.apache.commons.fileupload.disk.DiskFileItemFactory;  
  21. import org.apache.commons.fileupload.servlet.ServletFileUpload;  
  22. import org.apache.commons.fileupload.util.Streams;  
  23.    
  24.   
  25. public class Uploadify extends HttpServlet {  
  26.        
  27.     private static final long serialVersionUID = 1L;  
  28.   
  29.     /** 
  30.      * 实现多文件的同时上传 
  31.      */   
  32.     public void doGet(HttpServletRequest request,  
  33.             HttpServletResponse response) throws ServletException, IOException {  
  34.           
  35.         //设置接收的编码格式  
  36.         request.setCharacterEncoding("UTF-8");  
  37.         Date date = new Date();//获取当前时间  
  38.         SimpleDateFormat sdfFileName = new SimpleDateFormat("yyyyMMddHHmmss");  
  39.         SimpleDateFormat sdfFolder = new SimpleDateFormat("yyMM");  
  40.         String newfileName = sdfFileName.format(date);//文件名称  
  41.         String fileRealPath = "";//文件存放真实地址  
  42.           
  43.         String fileRealResistPath = "";//文件存放真实相对路径  
  44.           
  45.         //名称  界面编码 必须 和request 保存一致..否则乱码  
  46.         String name = request.getParameter("name");  
  47.               
  48.            
  49.         String firstFileName="";  
  50.         // 获得容器中上传文件夹所在的物理路径  
  51.         String savePath = this.getServletConfig().getServletContext().getRealPath("/") + "uploads\\" + newfileName +"\\";  
  52.         System.out.println("路径" + savePath);  
  53.         File file = new File(savePath);  
  54.         if (!file.isDirectory()) {  
  55.             file.mkdir();  
  56.         }  
  57.   
  58.         try {  
  59.             DiskFileItemFactory fac = new DiskFileItemFactory();  
  60.             ServletFileUpload upload = new ServletFileUpload(fac);  
  61.             upload.setHeaderEncoding("UTF-8");  
  62.             // 获取多个上传文件  
  63.             List fileList = fileList = upload.parseRequest(request);  
  64.             // 遍历上传文件写入磁盘  
  65.             Iterator it = fileList.iterator();  
  66.             while (it.hasNext()) {  
  67.                 FileItem item = (FileItem) it.next();  
  68.                   
  69.                 // 如果item是文件上传表单域     
  70.                 // 获得文件名及路径     
  71.                 String fileName = item.getName();  
  72.                 if (fileName != null) {  
  73.                     firstFileName=item.getName().substring(item.getName().lastIndexOf("\\")+1);  
  74.                     String formatName = firstFileName.substring(firstFileName.lastIndexOf("."));//获取文件后缀名  
  75.                     fileRealPath = savePath + newfileName+ formatName;//文件存放真实地址  
  76.                       
  77.                     BufferedInputStream in = new BufferedInputStream(item.getInputStream());// 获得文件输入流  
  78.                     BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(new File(fileRealPath)));// 获得文件输出流  
  79.                     Streams.copy(in, outStream, true);// 开始把文件写到你指定的上传文件夹  
  80.                     //上传成功,则插入数据库  
  81.                     if (new File(fileRealPath).exists()) {  
  82.                         //虚拟路径赋值  
  83.                         fileRealResistPath=sdfFolder.format(date)+"/"+fileRealPath.substring(fileRealPath.lastIndexOf("\\")+1);  
  84.                         //保存到数据库  
  85.                         System.out.println("保存到数据库:");  
  86.                         System.out.println("name:"+name);  
  87.                         System.out.println("虚拟路径:"+fileRealResistPath);  
  88.                     }  
  89.                        
  90.                 }   
  91.             }   
  92.         } catch (FileUploadException ex) {  
  93.             ex.printStackTrace();  
  94.             System.out.println("没有上传文件");  
  95.             return;  
  96.        }   
  97.        response.getWriter().write("1");  
  98.           
  99.     }  
  100.    
  101.     public void doPost(HttpServletRequest req, HttpServletResponse resp)  
  102.             throws ServletException, IOException {  
  103.         doGet(req, resp);  
  104.     }  
  105. }  

 

web.xml

 

 

Xml代码   收藏代码
  1. <servlet>  
  2.  <servlet-name>Uploadify</servlet-name>  
  3.  <servlet-class>com.Uploadify</servlet-class>  
  4. </servlet>  
  5. <servlet-mapping>  
  6.  <servlet-name>Uploadify</servlet-name>  
  7.  <url-pattern>/scripts/uploadify</url-pattern>  
  8. </servlet-mapping>  

 

原文www.iteye.com/blog/1550508

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值