多文件上传

来源:互联网 发布:罗马2全面战争修改数据 编辑:程序博客网 时间:2024/06/17 02:47

最近一直在做公司产品在线贴吧的多文件上传,虽然功能还没有完全做完,但是基本技术难题已经解决,现归纳总结如下:

一、使用struts2 + org.apache.commons.fileupload,在Action中

  1. ActionContext ac = ActionContext.getContext();  
  2.        HttpServletRequest request = (HttpServletRequest) ac.get       (ServletActionContext.HTTP_REQUEST);  
  3.        HttpServletResponse response = (HttpServletResponse) ac.get(ServletActionContext.HTTP_RESPONSE);  
  4.  
  5.        upload.parseRequest(request);  
  6.  

   这里upload.parseRequest(request);获不到值,原因为Struts2的过滤器将request过滤,从而改变了request的类型:

  1. <filter> 
  2.  <filter-name>struts2</filter-name> 
  3.  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
  4. </filter> 
  5. <filter-mapping> 
  6.  <filter-name>struts2</filter-name> 
  7.  <url-pattern>/*</url-pattern> 
  8. </filter-mapping> 

解决方法有两种:

1:WEB-INFO的web.xml中将struts2过滤器的配置改为:

  1. <filter> 
  2.   <filter-name>struts2</filter-name> 
  3.   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
  4.  </filter> 
  5.  <filter-mapping> 
  6.   <filter-name>struts2</filter-name> 
  7.   <url-pattern>*.action</url-pattern> 
  8.  </filter-mapping> 
  9.  

这种方法在我的项目中没有成功;

2:自己写一个过滤器:配置在sturts2的过滤器配置之前

  1. <filter> 
  2.         <filter-name>PersonalFilter</filter-name> 
  3.         <filter-class>com.huarui.web.servlet.PersonalFilter</filter-class> 
  4.     </filter> 
  5.     <filter-mapping> 
  6.         <filter-name>PersonalFilter</filter-name> 
  7.         <url-pattern>/user/postbar/BarUpload.action</url-pattern> 
  8.     </filter-mapping>  

 

  1. public class PersonalFilter implements Filter {  
  2.  
  3.     public void destroy() {  
  4.           
  5.     }  
  6.  
  7.     public void doFilter(ServletRequest request, ServletResponse response,  
  8.             FilterChain chain) throws IOException, ServletException {  
  9.         chain.doFilter(new StrutsRequestWrapper((HttpServletRequest) request), response);     
  10.     }  
  11.  
  12.     public void init(FilterConfig arg0) throws ServletException {  
  13.         // TODO Auto-generated method stub  
  14.           
  15.     }  
  16.  

注意映射的路径,主要是过滤这次请求,将request转型为StrutsRequestWrapper,获取值成功!

二、前台JS触发<input type='file'/>的onclick事件,在提交前input值清空,这是IE的安全机制,本身无法解决,但通过以下代码变相解决:

  1. <HTML> 
  2.     <BODY> 
  3.         <div id="tt" style="position:relative;"> 
  4.             <input type="button" value="添加附件" onmouseover="floatFile()"> 
  5.             <br> 
  6.             <div id="div1"> 
  7.                 <div id="file1text" ></div><input id="file1" name="myfile" type="file" onchange="showText(this)" style="position:absolute;filter:alpha(opacity=50);width:30px;" hidefocus> 
  8.             </div> 
  9.         </div> 
  10.         </p> 
  11.         <input type="button" onclick="alert($('tt').innerHTML)" value="showHTML"> 
  12.     </BODY> 
  13. </HTML> 
  14. <SCRIPT LANGUAGE="JavaScript"> 
  15.     function $(id)  
  16.     {  
  17.         return document.getElementById(id);  
  18.     }  
  19.     //全局变量,记录文件数;  
  20.     var fileNum=1;  
  21.     //mouseover时,把input file移到按扭上,保证点击的是file,  
  22.     function floatFile()  
  23.     {      
  24.         $("file"+fileNum).style.posTop=event.srcElement.offsetTop;  
  25.         $("file"+fileNum).style.posLeft=event.x-$("file"+fileNum).offsetWidth/2;  
  26.     }  
  27.     //选择完一个文件之后,自动创建一个新的div 和 file表单,用于下回使用,hidden刚用过的file  
  28.     function showText(obj)  
  29.     {  
  30.         $(obj.id+"text").innerHTML=obj.value+"&nbsp;&nbsp;<a href='javascript:del("+fileNum+")'>删除</a>";  
  31.         $("file"+fileNum).style.display='none';  
  32.         fileNumfileNum=fileNum+1;  
  33.         //直接追加innerHTML(innerHTML+=)会清空原来file中的内容  
  34.         $("div"+(fileNum-1)).insertAdjacentHTML('AfterEnd','<div id="div'+fileNum+'"><div id="file'+fileNum+'text" ></div><input id="file'+fileNum+'" name="myfile" type="file"  onchange="showText(this)"  style="position:absolute;filter:alpha(opacity=0);width:30px;"hidefocus></div>');  
  35.     }  
  36.     function del(id)  
  37.     {  
  38.         $("div"+id).innerHTML="";  
  39.         $("div"+id).style.display="none";  
  40.     }  
  41.  
  42. </SCRIPT> 
  43.  

画面难看但功能实现!

本文出自 “希望家园” 博客,请务必保留此出处http://dyq0908.blog.51cto.com/4347597/854881

0 0
原创粉丝点击