struts2 download

public class FileDownloadAction implements Action {

    private String inputPath;
    public void setInputPath(String value) {
        inputPath = value;
    }

    public InputStream getInputStream() throws Exception {
        return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
    }

    public String execute() throws Exception {
        return SUCCESS;
    }

}

 

<action name="download" class="org.apache.struts2.showcase.filedownload.FileDownloadAction">
            <param name="inputPath">/images/struts.gif</param>   //这个是和action中的属性对应
   <result name="success" type="stream">
                <param name="contentType">image/gif</param>
                <param name="inputName">inputStream</param>   //和getInputStream()对应
                <param name="contentDisposition">filename="struts.gif"</param>
                <param name="bufferSize">4096</param>
            </result>
        </action>

 

参数

说明

contentType

内容类型,和互联网MIME标准中的规定类型一致,例如text/plain代表纯文本,text/xml表示XML,image/gif代表GIF图片,image/jpeg代表JPG图片

 

用来做动态文件下载的,事先并不知道未来的文件类型是什么,那么我们可以把它的值设置成为: application/octet-stream;charset=ISO8859-1 ,注意一定要加入charset,否则某些时候会导致下载的文件出错

inputName

下载文件的来源流,对应着action类中某个类型为Inputstream的属性名,例如取值为inputStream的属性需要编写getInputStream()方法

contentDisposition

文件下载的处理方式,包括内联(inline)和附件(attachment)两种方式,而附件方式会弹出文件保存对话框,否则浏览器会尝试直接显示文件。取值为:

attachment;filename="struts2.txt",表示文件下载的时候保存的名字应为struts2.txt。如果直接写filename="struts2.txt",那么默认情况是代表inline,浏览器会尝试自动打开它,等价于这样的写法:inline; filename="struts2.txt"

bufferSize

下载缓冲区的大小

 

(1)中文文件名会出现乱码??

downFileName=new String(fileName.getBytes(), "ISO8859-1");

 -------------文件下载,绝对路径,相对路径都行-------我修改的代码如下:(可通用)

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;

/**
 * Demonstrates file resource download.
 * Set filePath to the local file resource to download,
 * relative to the application root ("/images/struts.gif").
 *
 */
public class FileDownloadAction implements Action {

    private String inputPath;
    private String fileName;
    public void setInputPath(String value) {
        inputPath = value;
    }

   
    public void setFileName(String fileName) {
  this.fileName = fileName;
  System.out.println(fileName);
 }


 public InputStream getInputStream() throws Exception {
  
  char c=inputPath.charAt(1);
     System.out.println((c==':'));
  if(!exists(c)){
   return null;
  }
     
     if(c==':'){
      
      return new java.io.FileInputStream(inputPath);//从系统磁盘文件读取数据
     }
     else{
       return ServletActionContext.getServletContext().getResourceAsStream(inputPath);//从项目webRoot下的目录下
     }
    }
    public String getDownloadFileName(){
     
     String downFileName = null;
     if(fileName==null){                                 //fileName可以不设初始值,默认原始文件名,分隔符为/
      int start=inputPath.lastIndexOf("/");
      fileName=inputPath.substring(start+1);
     }
      try {
    downFileName=new String(fileName.getBytes(), "ISO8859-1"); //对中文的处理
   } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
   }
     
     return downFileName;
    }
    private boolean exists(char c){
     File file=null;
     if(c==':'){
       file=new File(inputPath);
     }
     else{
      String realPath=ServletActionContext.getServletContext().getRealPath("/");
      file=new File(realPath+inputPath);
     }
            //文件不存在
     return file.exists();
    }
    public String execute() throws Exception {
        return SUCCESS;
    }

}
 <action name="download" class="org.apache.struts2.showcase.filedownload.FileDownloadAction">
        <!-- 
            <param name="inputPath">images/struts.gif</param>
   <result name="success" type="stream">
                <param name="contentType">image/gif</param>
                <param name="inputName">inputStream</param>
                <param name="contentDisposition">filename="struts.gif"</param>
                <param name="bufferSize">4096</param>
            </result>
           -->
            <param name="inputPath">c:/test二.doc</param>
            <param name="fileName"></param>
            <result type="stream">
              <param name="contentType">application/msword;charset=ISO8859-1</param>
                <param name="inputName">inputStream</param>
             <!--   使用经过转码的文件名作为下载文件名,downloadFileName属性
                对应action类中的方法 getDownloadFileName()  -->
                <param name="contentDisposition">attachment;filename="${downloadFileName}"</param>
                <param name="bufferSize">4096</param>
            </result>
           
        </action>

        <action name="download2" class="org.apache.struts2.showcase.filedownload.FileDownloadAction">
       
            <param name="inputPath">/images/struts-gif.zip</param>
            <!-- 相对webRoot目录下 -->
            <result name="success" type="stream">
                <param name="contentType">application/zip</param>
                <param name="inputName">inputStream</param>
                <param name="contentDisposition">filename="struts-gif.zip"</param>
                <param name="bufferSize">4096</param>
            </result>
        </action>
--------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值