struts2文件上传和下载

时间:2024-11-03 12:34:26

1. struts系统中的拦截器介绍

过滤器:javaweb中的服务器组件,主要针对的请求和响应进行拦截。

拦截器:主要针对方法的调用,进行拦截器,当使用代理对象调用某个方法时候

对方法的调用进行拦截,对拦截到的方法可以进行扩展,增强

Struts2中的拦截器主要针对Action中方法的调用进行拦截

 <interceptors>
<!—声明n个拦截器-->
<interceptor name="fileUpload"
class="org.apache.struts2.interceptor.FileUploadInterceptor" />
<interceptor name="i18n"
class="com.opensymphony.xwork2.interceptor.I18nInterceptor" />
<interceptor name="logger"
class="com.opensymphony.xwork2.interceptor.LoggingInterceptor" />
<interceptor name="modelDriven"
class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor" />
<interceptor name="params"
class="com.opensymphony.xwork2.interceptor.ParametersInterceptor" />
<interceptor name="staticParams"
class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor" />
<interceptor name="token"
class="org.apache.struts2.interceptor.TokenInterceptor" />
<interceptor name="tokenSession"
class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor" />
<!—由已经声明的n个拦截器,组成拦截器栈-->
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception" />
<interceptor-ref name="alias" />
<interceptor-ref name="servletConfig" />
<interceptor-ref name="i18n" />
<interceptor-ref name="prepare" />
<interceptor-ref name="chain" />
<interceptor-ref name="scopedModelDriven" />
<interceptor-ref name="modelDriven" />
<interceptor-ref name="fileUpload" />
<interceptor-ref name="checkbox" />
<interceptor-ref name="multiselect" />
<interceptor-ref name="staticParams" />
<interceptor-ref name="actionMappingParams" />
<interceptor-ref name="params">
<param name="excludeParams">^class\..*,^dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,^parameters\..*,^action:.*,^method:.*
</param>
</interceptor-ref>
<interceptor-ref name="conversionError" />
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="debugging" />
<interceptor-ref name="deprecation" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="defaultStack" />
</package>

拦截器栈:

a --> b --> c --> execute() --> c --> b --> a

2.文件上传

2.1 Action

 /*** 处理文件上传的请求 **/
public class FileAction
{
// org.apache.struts2.interceptor.FileUploadInterceptor
// 1 定义文件上传需要的特定属性
private File img; // 上传的文件对象
private String imgContentType;// 上传的文件的内容的类型
private String imgFileName;// 上传的文件名称
private String path; // 文件保存到服务器上的目录 public String getPath()
{
return path;
} public void setPath(String path)
{
this.path = path;
} public File getImg()
{
return img;
} public void setImg(File img)
{
this.img = img;
} public String getImgContentType()
{
return imgContentType;
} public void setImgContentType(String imgContentType)
{
this.imgContentType = imgContentType;
} public String getImgFileName()
{
return imgFileName;
} public void setImgFileName(String imgFileName)
{
this.imgFileName = imgFileName;
} /*** 处理文件上传的请求 ****/
public String uploadFile()
{
System.out.println(img);
System.out.println(imgContentType);
System.out.println(imgFileName);
// 修改文件名
String fname = UUID.randomUUID()
+ imgFileName.substring(imgFileName.lastIndexOf("."));
// 将上传的文件对象img保存到服务器的指定目录
// 1获得path的绝对路径
String pth = ServletActionContext.getServletContext().getRealPath(path);
// 2在pth下创建文件
File file = new File(pth, fname);
// 3将img内容拷贝file中
try
{
FileUtils.copyFile(img, file);
}
catch (IOException e)
{
e.printStackTrace();
}
return Action.SUCCESS;
}
}

2.2 struts.xml 配置 1 <?xml version="1.0" encoding="UTF-8"?>

  <!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="file" namespace="/" extends="struts-default">
<interceptors>
<!-- 自定义拦截器栈 -->
<interceptor-stack name="mystack">
<!-- 引用系统文件上传的拦截器 -->
<interceptor-ref name="fileUpload">
<param name="allowedExtensions">txt,jpg,doc</param>
<!-- 其他属性的赋值同理 -->
</interceptor-ref>
      <interceptor-ref name="staticParams"></interceptor-ref>
<!-- 引用系统基本拦截器栈 -->
<interceptor-ref name="basicStack" />
</interceptor-stack>
</interceptors>
<action name="fileAction_*" class="com.guangsoft.action.FileAction"
method="{1}">
<!-- 引用自定义的拦截器栈 -->
<interceptor-ref name="mystack"></interceptor-ref>
<!-- 给path属性赋值 -->
<param name="path">filedir</param>
<result>/index.jsp</result>
</action>
</package>
</struts>

2.3实现UI页面

 <form action="fileAction_uploadFile.action" method="post"
enctype="multipart/form-data">
<input type="file" name="img" />
<input type="submit" />
</form>

3.文件下载

3.1 Action

 //org.apache.struts2.dispatcher.StreamResult
public class DownloadAction
{
private InputStream inputStream;
private String fname;// 需要下载的文件的名字 public InputStream getInputStream()
{
System.out.println("----------getInputStream");
return inputStream;
} public String getFname()
{
return fname;
} public void setFname(String fname)
{
this.fname = fname;
} /*** 实现文件下载 ***/
public String download()
{
System.out.println("----------download");
// 获得附件的保存目录
String path = ServletActionContext.getServletContext().getRealPath(
"filedir");
// 将下载的文件封装为InputStream的对象
try
{
inputStream = new FileInputStream(path + "/" + fname);
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return Action.SUCCESS;
}
}

3.2 struts.xml 配置:

 <package name="down" namespace="/down" extends="struts-default">
<action name="downloadAction_*" class="com.guangsoft.action.DownloadAction"
method="{1}">
<result type="stream">
<param name="inputName">inputStream</param>
<!-- 指定下载的附件的类型 -->
<param name="contentType">application/octet-stream</param>
<!-- 指定下载的附件的名称 -->
<param name="contentDisposition">attachment;filename=${fname}</param>
</result>
</action>
</package>