struts2没有提供自己的请求解析器,也就是说,struts2不会自己去处理multipart/form-data的请求,它需要调用其他请求解析器,将HTTP请求中的表单域解析出来,但struts2在原有的上传解析器上作了进一步封装,更进一步简化了文件上传,Struts2的struts.properties配置文件中,配置struts2的上传文件解析器struts.multipart.parser=jakarta(srtuts2默认),也可以设置为常用的cos,pell等。
struts2实现上传下载所必须的2个jar包:commons-fileupload-xxx.jar和commons-io-xxx.jar。
一、文件上传
1. 单文件上传
(1)单文件上传表单视图:
-
<body>
-
<form action="/test/upload.action" enctype="multipart/form-data" method="post">
-
<input name="uploadfile" type="file">
-
<input type="submit" value="上传">
-
</form>
-
</body>
注意:表单必须设置enctype="multipart/form-data"和method="post"属性,否则上传会出错。
2. 单文件上传Action类
-
-
-
-
-
-
public class UploadAction extends ActionSupport{
-
-
<!--获取上传文件,名称必须和表单file控件名相同-->
-
private File uploadfile;
-
-
<!--获取上传文件名,命名格式:表单file控件名+FileName(固定)-->
-
private String uploadfileFileName;
-
-
-
private String uploadfileContentType;
-
-
...
-
-
-
public String upload() throws IOException
-
{
-
-
String realpath = ServletActionContext.getServletContext().getRealPath("/image");
-
-
-
if(uploadfile!=null)
-
{
-
-
File savefile = new File(realpath,uploadfileFileName);
-
-
-
if(!savefile.getParentFile().exists())
-
savefile.getParentFile().mkdirs();
-
-
-
FileUtils.copyFile(uploadfile,savefile);
-
-
-
ActionContext.getContext().put("message", "上传成功!");
-
}
-
return "success";
-
}
-
-
-
public String upload() throws IOException
-
{
-
FileOutputStream fos = new FileOutputStream("D:\\"+uploadfileFileName);
-
-
FileInputStream fis = new FileInputStream(uploadfile);
-
-
byte[] buffer = new byte[1024];
-
-
int len = 0;
-
-
while((len=fis.read(buffer))>0)
-
{
-
fos.write(buffer,0,len);
-
}
-
return "success";
-
}
-
-
}
注意:记得Action类要继承ActionSupport。
3. 配置struts.xml文件
-
<package name="hello" namespace="/test" extends="struts-default">
-
<action name="upload" class="action.UploadAction" method="upload">
-
-
-
<constant name="struts.custom.i18n.resources" value="struts"></constant>
-
-
-
<interceptor-ref name="fileUpload">
-
-
-
<param name="allowedTypes">image/bmp,image/gif,image/jpg</param>
-
-
-
<param name="allowedExtensions">txt,excel,ppt</param>
-
-
-
<param name="maximumSize">20480</param>
-
-
</interceptor-ref>
-
-
-
<interceptor-ref name="defaultStack"></interceptor-ref>
-
-
<result>/success.jsp</result>
-
-
-
<result name="input">/index.jsp</result>
-
-
</action>
-
</package>
注意:(1) 拦截器实现要在UploadAction类中继承ActionSupport,否则拦截器无效。
(2) 必须显示配置引用Struts默认的拦截器栈:defaultStack
(3) struts的默认上传文件大小为2M,可以用常量来控制扩大上传限制:
<constant name="struts.multipart.maxSize" value="104857600"></constant> //设置上传上限为10M
真正的视频网站会使用插件而不是这种web上传的方式上传文件,因为Web上传的稳定性差。
(4) 在失败的页面视图中使用<s:fielderror/>标签可以输出上传失败的原因,错误信息默认是struts提供的英文字符,如果需要将其转换为中文字符,可在国际化资源文件中配置以下代码:
struts.properties:
-
struts.messages.error.content.type.not.allowed = 上传的文件类型不正确
-
struts.messages.error.file.too.large= 上传的文件太大
-
struts.messages.error.uploading=上传时出现未知错误
2. 多文件上传:
多文件上传原理同单文件上传,主要是在action中定义File[]数组来接收多文件数据。
(1)多文件上传表单视图:
-
<body>
-
<s:form action="multiUpload" method="post" namespace="/test3" enctype="multipart/form-data">
-
<s:file name="uploadfile" label="文件1"></s:file>
-
<s:file name="uploadfile" label="文件2"></s:file>
-
<s:file name="uploadfile" label="文件3"></s:file>
-
<s:file name="uploadfile" label="文件4"></s:file>
-
<s:submit value="上传"></s:submit>
-
</s:form>
-
</body>
(2)多文件上传Action类:
-
public class UploadAction extends ActionSupport {
-
private File[] uploadfile;
-
private String[] uploadfileFileName;
-
private String[] uploadfileContentType;
-
-
...
-
-
-
public String upload() throws IOException {
-
String realpath = ServletActionContext.getServletContext().getRealPath("/image");
-
if (uploadfile != null) {
-
File savepath = new File(realpath);
-
if (!savepath.exists())
-
savepath.mkdirs();
-
for (int i = 0; i < uploadfile.length; i++) {
-
File savefile = new File(realpath, uploadfileFileName[i]);
-
FileUtils.copyFile(uploadfile[i], savefile);
-
}
-
ActionContext.getContext().put("message", "上传成功!");
-
}
-
return "success";
-
}
-
-
-
public String upload() throws IOException
-
{
-
for(int i = 0 ; i < uploadfile.length; i ++)
-
{
-
FileOutputStream fos = new FileOutputStream("D:\\"+uploadfileFileName[i]);
-
-
FileInputStream fis = new FileInputStream(uploadfile[i]);
-
-
byte [] buffer = new byte[1024];
-
-
int len = 0 ;
-
-
while((len = fis.read(buffer))>0)
-
{
-
fos.write(buffer,0,len);
-
}
-
-
}
-
return "success";
-
}
-
-
}
二、文件下载
(1)文件下载Action类
-
public class DownloadAction extends ActionSupport {
-
-
-
private String downloadPath;
-
-
-
private String filename;
-
-
-
private String savePath;
-
-
public String getSavePath() {
-
return savePath;
-
}
-
-
public void setSavePath(String savePath) {
-
this.savePath = savePath;
-
}
-
-
public String getFilename() {
-
return filename;
-
}
-
-
public void setFilename(String filename) {
-
this.filename = filename;
-
}
-
-
public void setDownloadPath(String downloadPath) {
-
this.downloadPath = downloadPath;
-
}
-
-
-
public InputStream getTargetFile() throws FileNotFoundException
-
{
-
return new FileInputStream(downloadPath);
-
}
-
-
public String execute()
-
{
-
return "success";
-
}
-
-
}
(2)配置struts.xml文件:
-
<package name="test3_download" extends="struts-default">
-
<action name="download" class="com.action.DownloadAction" method="execute">
-
-
-
<param name="downloadPath">${savePath}</param>
-
-
-
<result name="success" type="stream">
-
-
-
<param name="inputName">targetFile</param>
-
-
-
-
-
-
<param name="contentDisposition">attachment;filename=${filename}</param>
-
-
-
<param name="bufferSize">2048</param>
-
</result>
-
</action>
-
</package>