webwork2中多文件上传的处理

webwork2中多文件上传的处理  作者:onebody(onebodysoftware@hotmail.com)
1. webwork-default.xml 配置 (拦截器的配置)

 

< interceptors >
    
< interceptor  name ="fileUpload"  class ="com.opensymphony.webwork.interceptor.FileUploadInterceptor" />

    
<!--  Sample file upload stack  -->
    
< interceptor-stack  name ="fileUploadStack" >
        
< interceptor-ref  name ="fileUpload" />
        
< interceptor-ref  name ="basicStack" />
    
</ interceptor-stack >

</ interceptors >

 


2. xwork.xml 配置 (处理的Action配置)

< package  name ="fileUpload"  namespace ="fileUpload"  extends ="webwork-default" >
 
< action  name ="fileUpload"  class ="com.omyos.fileUpload.UploadAction" >
     
< result  name ="main" > fileUploadIndex.jsp </ result >
            
< interceptor-ref  name ="fileUpload" />
        
</ action >  
</ package >

 

3. webwork.properties 配置

/*多文件上传  单个文件上传时直接用pell就行了 
单文件与多文件上传的处理有所不同详细Action类中的描述*/

webwork.multipart.parser=jakarta   


  

3. fileUploadIndex.jsp 代码

 

< html >
< head >< title > 多文件上传实例 </ title ></ head >
< script language = " Javascript "  charset = " utf-8 " >
    function addAccessory() 
{
        var accessoryListObj 
= document.getElementById("accessoryList");
        var accObj 
= document.createElement("span");
        var spanId 
= (new Date()).getTime();
        var spanHTML 
= " <input name=files type=file width=100px/><input type=button value=删除 οnclick=deleteFile('" + spanId + "')><br>";

        accObj.setAttribute(
"id", spanId);
        accObj.setAttribute(
"width""100px");
        accObj.innerHTML 
= spanHTML;
        accessoryListObj.appendChild(accObj);
    }


    function deleteFile(fileId) 
{
        var accessoryListObj 
= document.getElementById("accessoryList");
        var accObj 
= document.getElementById(fileId);
        accessoryListObj.removeChild(accObj);
    }

</ script >
< body >
   
< form action = " fileUpload/fileUpload.action "   enctype = " multipart/form-data "  method = " post " >  
 
< table id = " DataTable " >
                            
< tbody >
                                
< tr >
                                    
< td  class = " style2 "  colspan = " 4 " >< strong > 附件:  </ strong >
                                    
< span title = " 增加附件 "
                                          onclick
= " addAccessory() "
                                          style
= " cursor:pointer;color:blue; " >< strong > [添加附件] </ strong ></ span >
                                    
</ td >
                                
</ tr >
                                
< tr >
                                    
< td colspan = " 4 "  style = " width:100%;height:300px; " >
                                        
< div id = " accessoryList "
                                             style
= " overflow-x:hidden;overflow-y:auto;width:100%;height:100%; " >
                                             
& nbsp;
                                        
</ div >
                                    
</ td >
                                
</ tr >
                                
< tr >
                                    
< td colspan = " 4 "  style = " width:100%;height:15px; "  align = " center " >
                                        
< input type = " Submit "  name = " 提交 " >
                                        
                                    
</ td >
                                
</ tr >
                            
</ tbody >
                        
</ table >
   
</ form >
</ body >
</ html >


4.com.omyos.fileUpload.UploadAction.java 代码

 

public   class  DataInfoAction extends ActionSupport {
    
private File[] files;             //多个文件对象数组
    private String[] filesFileName;   //文件对应的真实文件名  单个文件上传时不需要此属性定义 
                                      
//多文件上传时 file.getName() 无效,取得不是真实的文件名,而是upload_xxx.tmp

    
public String execute() {
        
if (files != null{
            
for (int i = 0; i < files.length; i++{
                File file 
= files[i];
                String fileName 
= filesFileName[i];
                System.
out.print(" File list: ");
                System.
out.print("   name:=" + file.getName() + " ");
                System.
out.print("   fileName:=" + fileName + " ");
                System.
out.print("   path:=" + file.getPath() + " ");
                System.
out.print("  ");

  
//文件上传的代码自己code吧...
            }

        }

        
return SUCCESS;
    }


    
public String[] getFilesFileName() {
        
return filesFileName;
    }


    
public void setFilesFileName(String[] filesFileName) {
        
this.filesFileName = filesFileName;
    }


    
public File[] getFiles() {
        
return files;
    }


    
public void setFiles(File[] files) {
        
this.files = files;
    }


    
/**
     * 获取session对象
     * @return
     
*/

    
public HttpSession getSession() {
        
return getRequest().getSession();
    }


    
/**
     * 获取request对象
     *
     * @return httpServletRequest
     
*/

    
public HttpServletRequest getRequest() {
        
return ServletActionContext.getRequest();
    }


    
/**
     * 获取response对象
     *
     * @return HttpServletResponse
     
*/

    
public HttpServletResponse getResponse() {
        
return ServletActionContext.getResponse();
    }


    
/**
     * 获取ServletContext对象
     *
     * @return ServletContext
     
*/

    
public ServletContext getContext() {
        
return ServletActionContext.getServletContext();
    }


    
/**
     * 获取ActionContext
     *
     * @return
     
*/

    
public ActionContext getActionContext() {
        
return ActionContext.getContext();
    }


    
/**
     * 获取参数map
     *
     * @return
     
*/

    
public Map getParameterMap() {
        
return getActionContext().getSession();
    }


    
/**
     * 获取输入参数名字对应的值
     *
     * @param name
     * @return
     
*/

    
public String getParameter(String name) {
        
return (String) getParameterMap().get(name);
    }

}


 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值