struts2没有提供自己的请求解析器,也就是说,struts2不会自己区处理multipart/form-data的请求,它需要调用其他请求解析器,将HTTP请求中的表单域解析出来,但struts2在原有的上传解析器上作了进一步封装,更进一步简化了文件上传
Struts2的struts.properties配置文件中,配置struts2的上传文件解析器
struts.multipart.parser=jakarta (srtuts2默认),也可以设置为常用的cos,pell等
配置上传页面:
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=GBK" />
< title > 简单的文件上传 </ title >
</ head >
< body >
< form action ="upload.action" method ="post" enctype ="multipart/form-data" >
文件标题: < input type ="text" name ="title" />< br >
选择文件: < input type ="file" name ="upload" />< br >
< input value ="上传" type ="submit" />
</ form >
</ body >
</ html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=GBK" />
< title > 简单的文件上传 </ title >
</ head >
< body >
< form action ="upload.action" method ="post" enctype ="multipart/form-data" >
文件标题: < input type ="text" name ="title" />< br >
选择文件: < input type ="file" name ="upload" />< br >
< input value ="上传" type ="submit" />
</ form >
</ body >
</ html >
配置上传action
package
lee;
import com.opensymphony.xwork2.Action;
import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io. * ;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport
... {
private String title;
private File upload;
private String uploadContentType;
private String uploadFileName;
//接受依赖注入的属性
private String savePath;
//接受依赖注入的方法
public void setSavePath(String value)
...{
this.savePath = value;
}
private String getSavePath() throws Exception
...{
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setTitle(String title) ...{
this.title = title;
}
public void setUpload(File upload) ...{
this.upload = upload;
}
public void setUploadContentType(String uploadContentType) ...{
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(String uploadFileName) ...{
this.uploadFileName = uploadFileName;
}
public String getTitle() ...{
return (this.title);
}
public File getUpload() ...{
return
import com.opensymphony.xwork2.Action;
import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io. * ;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport
... {
private String title;
private File upload;
private String uploadContentType;
private String uploadFileName;
//接受依赖注入的属性
private String savePath;
//接受依赖注入的方法
public void setSavePath(String value)
...{
this.savePath = value;
}
private String getSavePath() throws Exception
...{
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setTitle(String title) ...{
this.title = title;
}
public void setUpload(File upload) ...{
this.upload = upload;
}
public void setUploadContentType(String uploadContentType) ...{
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(String uploadFileName) ...{
this.uploadFileName = uploadFileName;
}
public String getTitle() ...{
return (this.title);
}
public File getUpload() ...{
return