JavaWeb Response提供檔案下載功能

來源:互聯網
上載者:User

JavaWeb Response提供檔案下載功能

webapp項目的結構如:

download.html檔案的內容如下:

<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title></head><body>        <h1>資源下載:</h1>       <p> 單純地使用a標籤時,只有瀏覽器不能解析的檔案才會是下載,否則將被瀏覽器直接解析。</p>        <a href="/WEB/resource/a.mp3" >a.mp3</a><br>        <a href="/WEB/resource/a.exe" >a.exe</a><br>        <a href="/WEB/resource/a.txt" >a.txt</a><br>        <a href="/WEB/resource/a.xlsx" >a.xlsx</a><br>        <a href="/WEB/resource/a.png" >a.png</a><br>        <p>因此,使用a標籤結合servlet的response指示瀏覽器不解析這些待下載檔案</p>        <a href="/WEB/download?filename=a.mp3" >a.mp3</a><br>        <a href="/WEB/download?filename=a.exe" >a.exe</a><br>        <a href="/WEB/download?filename=a.txt" >a.txt</a><br>        <a href="/WEB/download?filename=a.xlsx" >a.xlsx</a><br>        <a href="/WEB/download?filename=a.png" >a.png</a><br></body></html>

負責處理下載的Servlet——download.java檔案的內容如下:

package com.download.servlet;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class Download */public class Download extends HttpServlet {    private static final long serialVersionUID = 1L;    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        //1.擷取請求下載的檔案名稱        String filename = request.getParameter("filename");        //2.擷取檔案的檔案系統路徑        String filePath = request.getServletContext().getRealPath("resource/"+filename);        //3.設定回應標頭,提示瀏覽器不要解析響應的檔案資料,而是以附件(attachment)的形式解析,即下載功能        response.setContentType(this.getServletContext().getMimeType(filename));        response.setHeader("Content-Disposition", "attachment;filename="+filename);        //4.讀取檔案的 輸入資料流,以及響應的輸出資料流,並將資料輸出給用戶端        InputStream in = new FileInputStream(filePath);        ServletOutputStream out = response.getOutputStream();        int len = 0;        byte[] buf = new byte[1024];        while((len=in.read(buf))!=-1) {            out.write(buf, 0, len);        }        in.close();    }    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        doGet(request, response);    }}

在瀏覽器地址欄中輸入http://localhost:8080/DownloadServlet/download.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.