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>  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM