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 >
< 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 >
< 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 >
< 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);
}
}
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);
}
}