Struts2:文件上传下载

来源:互联网 发布:首席数据官实战 编辑:程序博客网 时间:2024/05/02 00:05

文件上传:Struts2的文件上传建立在jarkata或者cos项目之上的,通过fileupload拦截器提供了更高层次的抽象,把文件直接当成一个域。

第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jarcommons-io-1.3.2.jar。这两个文件可以从http://commons.apache.org/下载。

第二步:把form表的enctype设置为:“multipart/form-data“,如下:

<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/xxx.action" method="post">

 <input  type="file"name="uploadImage">

</form>

第三步:在Action类中添加以下属性,属性红色部分对应于表单中文件字段的名称

public class HelloWorldAction{

 private File uploadImage;//得到上传的文件

 private String uploadImageContentType;//得到文件的类型

 private String uploadImageFileName;//得到文件的名称

 //这里略省了属性的getter/setter方法

 public Stringupload() throws Exception{

  Stringrealpath =ServletActionContext.getServletContext().getRealPath("/images");

  Filefile = new File(realpath);

  if(!file.exists())file.mkdirs();

  FileUtils.copyFile(uploadImage,new File(file,uploadImageFileName));

  return"success";

 }

}

如果是上传多个文件,使用数组或者list接收都可以

<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/xxx.action" method="post">

 <input  type="file"name="uploadImages">

 <input  type="file"name="uploadImages">

</form>

Action类中添加以下属性,属性红色部分对应于表单中文件字段的名称

public class HelloWorldAction{

 private File[] uploadImages;//得到上传的文件

 private String[] uploadImagesContentType;//得到文件的类型

 private String[] uploadImagesFileName;//得到文件的名称

 //这里略省了属性的getter/setter方法

 public Stringupload() throws Exception{

  Stringrealpath =ServletActionContext.getServletContext().getRealPath("/images");

  Filefile = new File(realpath);

  if(!file.exists())file.mkdirs();

  for(inti=0 ;i<uploadImages.length;i++){File uploadImage = uploadImages[i];

    FileUtils.copyFile(uploadImage, new File(file,uploadImagesFileName[i]));

}

  return"success";

 }}



文件下载:Struts2文件下载主要利用拦截器和stream结果类型,action中核心的只需要提供一个输入流

  1.  //返回一个输入流,作为一个客户端来说是一个输入流,但对于服务器端是一个 输出流  
  2.     public InputStream getDownloadFile() throws Exception  
  3.     {  
  4.         if(1 == number)  
  5.         {  
  6.            this.fileName = "Dream.jpg" ;  
  7.            //获取资源路径  
  8.            return ServletActionContext.getServletContext().getResourceAsStream("upload/Dream.jpg") ;  
  9.         }  
  10.           
  11.         else if(2 == number)  
  12.         {  
  13.             this.fileName = "jd2chm源码生成chm格式文档.rar" ;  
  14.             //解解乱码  
  15.             this.fileName = new String(this.fileName.getBytes("GBK"),"ISO-8859-1");  
  16.             return ServletActionContext.getServletContext().getResourceAsStream("upload/jd2chm源码生成chm格式文档.rar") ;  
  17.         }  
  18.         else  
  19.            return null ;  
  20.     } 

  21. Struts.xml中的配置
  1. <package name="struts2" extends="struts-default">        
  2.        <action name="FileDownload" class="com.struts2.filedownload.FileDownload">  
  3.            <result name="success" type="stream">  
  4.                <param name="contentType">text/plain</param>  
  5.                <param name="contentDisposition">attachment;fileName="${fileName}"</param>  
  6.                <param name="inputName">downloadFile</param>  
  7.                <param name="bufferSize">1024</param>  
  8.            </result>  
  9.        </action>  
  10.      
  11.    </package> 


  1. 1.结果类型必须要写成 type="stream"  ,与之对应的处理类是 org.apache.struts2.dispatcher.StreamResult


    2.涉及到的参数:


    3.

    1)  <param name="contentDisposition">attachment;fileName="${fileName}"</param>

         contentDisposition默认是 inline(内联的), 比如说下载的文件是文本类型的,就直接在网页上打开,不能直接打开的才会打开下载框自己选择

    2)  attachment :下载时会打开下载框

    3)  fileName="${fileName}" :在这定义的名字是一个动态的,该名字是显示在下载框上的文件名字


    4.<param name="inputName">downloadFile</param>,这个downloadFile名字要和FileDownload.java类中的getDownloadFile()方法名去掉get 一致



0 0
原创粉丝点击