下載文件時HttpServletResponse設置響應頭的Content-Disposition屬性


Content-Disposition屬性有兩種類型

  1. inline :將文件內容直接顯示在頁面
  2. attachment:彈出對話框讓用戶下載

彈出對話框下載文件

resp.setHeader("Content-Disposition", "attachment; filename="+fileName);

web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 5         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
 6     id="WebApp_ID" version="3.0">
 7   <display-name>DownloadDemo</display-name>
 8   <welcome-file-list>
 9     <welcome-file>index.jsp</welcome-file>
10   </welcome-file-list>
11   
12   <servlet>
13       <servlet-name>download</servlet-name>
14       <servlet-class>com.qf.servlet.DownloadServlet2</servlet-class>
15   </servlet>
16   <servlet-mapping>
17       <servlet-name>download</servlet-name>
18       <url-pattern>/download</url-pattern>
19   </servlet-mapping>
20 </web-app>

servlet類

 1 public class DownloadServlet2 extends HttpServlet {
 2 
 3     @Override
 4     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 5         //獲取文件在tomcat下的路徑
 6         String path = getServletContext().getRealPath("download/bb.txt");
 7 
 8         //讓瀏覽器收到這份資源的時候, 以下載的方式提醒用戶,而不是直接展示
 9         resp.setHeader("Content-Disposition", "attachment; filename=bb.txt");
10         
11         //轉化成輸入流
12         InputStream is = new FileInputStream(path);
13         OutputStream os = resp.getOutputStream();
14         
15         int len = 0;
16         byte[] bts = new byte[1024];
17         while ((len = is.read(bts)) != -1) {
18             os.write(bts, 0, len);
19         }
20         os.close();
21         is.close();
22     }
23 
24     @Override
25     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
26         doGet(req, resp);
27     }
28 }

url:http://localhost:8080/DownloadDemo/download

直接在瀏覽器頁面打開文件

resp.setHeader("Content-Disposition", "inline; filename="+fileName);

修改servlet類

 1 public class DownloadServlet2 extends HttpServlet {
 2 
 3     @Override
 4     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 5         //獲取文件在tomcat下的路徑
 6         String path = getServletContext().getRealPath("download/bb.txt");
 7 
 8         //讓瀏覽器收到這份資源的時候, 以下載的方式提醒用戶,而不是直接展示
 9         resp.setHeader("Content-Disposition", "inline; filename=bb.txt");
10         
11         //轉化成輸入流
12         InputStream is = new FileInputStream(path);
13         OutputStream os = resp.getOutputStream();
14         
15         int len = 0;
16         byte[] bts = new byte[1024];
17         while ((len = is.read(bts)) != -1) {
18             os.write(bts, 0, len);
19         }
20         os.close();
21         is.close();
22     }
23 
24     @Override
25     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
26         doGet(req, resp);
27     }
28     
29 }

 

 


免責聲明!

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



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