Struts2学习笔记--使用Response下载文件和Struts2的StreamResult文件下载

  • 使用Response下载文件,servlet中的文件下载是通过流来实现的

      我在webRoot文件夹下新建了一个文件夹from,里边放了一张图片,这里就以下载这张图片为例:download.jsp很简单,只有一个a标签.

    DownloadAction如下:

package com.wang.action; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.Action; public class DownloadAction { public String execute() throws IOException{ //获取response和request
        HttpServletRequest request=ServletActionContext.getRequest(); HttpServletResponse response=ServletActionContext.getResponse(); //获取要下载的文件夹路径
        String path=request.getRealPath("/from"); File file=new File(path,"IMG_0968.jpg"); response.setContentLength((int)file.length()); response.setCharacterEncoding("utf-8"); //此contenttype意思是不知道文件类型,使用二进制流的方式传输
        response.setContentType("application/octet-stream"); //注意,filename=后面的内容不加引号,我第一次加了引号,结果出错
        response.setHeader("Content-Disposition", "attachment;filename=IMG_0968.jpg"); InputStream is=new FileInputStream(file); //通过response获得输出流
        OutputStream os=response.getOutputStream(); byte[] b=new byte[1024]; int len=0; while((len=is.read(b))!=-1){ os.write(b, 0, len); } is.close(); os.close(); System.out.println("success download"); //注意:这里返回的是null
        return null; } }
DownloadAction

    这是使用servlet的reqsponse下载文件的方式,注意在action中,返回值是null,配置文件中不需要result标签,download.jsp和struts.xml省略.

  • 使用Struts2的StreamResult进行文件下载.

    webRoot文件夹下新建了一个文件夹from,里边放了两张图片分别是IMG_0968.jpg和IMG_0975.jpg.以下载这两张图片为例:

    downLoad.jsp页面:

<body>
   <a href="streamDownload.action?fileName=IMG_0968.jpg">流的方式下载第一张图片</a><br>
   <a href="streamDownload.action?fileName=IMG_0975.jpg">流的方式下载第二张图片</a>
  
  </body>

    StreamDownloadAction页面:

package com.wang.action; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class StreamDownload extends ActionSupport{ private String fileName; @Override public String execute() throws Exception { return SUCCESS; } public InputStream getInputStream() throws IOException{ HttpServletRequest request = ServletActionContext.getRequest(); String path=request.getRealPath("/from"); InputStream is=new FileInputStream(new File(path,fileName)); return is; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } }
StreamDownloadAction

    struts.xml页面:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>

<package name="default" namespace="/" extends="struts-default">
    <action name="streamDownload" class="com.wang.action.StreamDownload">
        <result type="stream">
            <!-- inputName默认就是InputStream,可以省略,也可以自己修改,如改成aaa, 则Action里的方法也要改成public InputStream getAaa() -->
            <param name="inputName">InputStream</param>
            <param name="contentDisposition">attachment;fileName=${fileName}</param>
        </result>
    </action>
</package>
</struts>  

 

你可能感兴趣的:(Struts2学习笔记--使用Response下载文件和Struts2的StreamResult文件下载)