JavaWeb-Demo2:文件下載

Github:file_download

一. 需求

  1. 頁面顯示超鏈接
  2. 點擊超鏈接後彈出下載提示框(Chrome 瀏覽器是直接下載)
  3. 完成圖片文件下載

二. 分析

  1. 點擊超鏈接,超鏈接指向的資源如果能夠被瀏覽器解析,則直接在瀏覽器中展示;如果不能,則彈出下載提示框。–> 不滿足需求,需求爲任何資源都要彈出下載提示框。
  2. 設置響應頭改變資源的的打開方式:Content-dispostion:attachment;filename=xxx ,以附件形式打開。

三. 開發步驟

  1. 定義 download.html 頁面,編輯超鏈接的 href 屬性指向 Servlet,並傳遞資源名稱 filename 作爲參數。

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>download</title>
    </head>
    <body>
        <a href="/file_download/img/九尾.jpg">圖片1</a>
        <a href="/file_download/img/2.jpg">圖片2</a>
        <hr />
        <a href="/file_download/downloadServlet?filename=九尾.jpg">圖片1</a>
        <a href="/file_download/downloadServlet?filename=2.jpg">圖片2</a>
    </body>
    </html>
    
  2. 定義 Servlet

    1. 獲取資源名稱
    2. 使用字節輸入流加載文件進內存
      1. 找到文件的真實(服務器)路徑
      2. 用字節流關聯文件
    3. 設置響應頭:setHeader("Content-dispostion", "attachment;filename=xxx")
    4. 將數據寫出到 response 輸出流
  3. 解決中文文件名問題:導入 DownLoadUtils 工具類

    1. 獲取客戶端使用的瀏覽器版本信息
    2. 根據不同的版本信息,響應不同的數據(設置 filename 的編碼方式)。

DownloadServlet.java

package com.hjplz.web.download;

import com.hjplz.web.utils.DownLoadUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

@WebServlet("/downloadServlet")
public class DownloadServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1. 獲取請求參數(文件名稱)
        String filename = request.getParameter("filename");

        // 2. 使用字節輸入流加載文件進內存
        // 2.1 找到文件的真實(服務器)路徑
        ServletContext servletContext = this.getServletContext();
        String realPath = servletContext.getRealPath("/img/" + filename);
        // 2.2 用字節流關聯文件
        FileInputStream fis = new FileInputStream(realPath);

        // 3. 設置響應頭
        // 3.1 設置響應頭類型:Content-Type
        String mimeType = servletContext.getMimeType(filename); // 獲取文件的 MIME 類型
        response.setHeader("Content-Type", mimeType);
        // 3.2 設置響應頭打開方式:Content-disposition

        // 解決中文文件名問題
        // 1. 獲取 User-Agent 請求頭
        String agent = request.getHeader("User-Agent");
        // 2. 使用 DownLoadUtils 工具類方法編碼文件名
        filename = DownLoadUtils.getFileName(agent, filename);

        response.setHeader("Content-disposition", "attachment;filename=" + filename);

        // 4. 將輸入流的數據寫出到輸出流中
        ServletOutputStream sos = response.getOutputStream();
        byte[] buff = new byte[1024 * 8];
        int len = 0;
        while((len = fis.read(buff)) != -1) {
            sos.write(buff, 0, len);
        }
        fis.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

DownLoadUtils.java

package com.hjplz.web.utils;

import java.util.Base64;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


public class DownLoadUtils {

    public static String getFileName(String agent, String filename) throws UnsupportedEncodingException {
        if (agent.contains("MSIE")) {
            // IE瀏覽器
            filename = URLEncoder.encode(filename, "utf-8");
            filename = filename.replace("+", " ");
        } else if (agent.contains("Firefox")) {
            // 火狐瀏覽器
            Base64.Encoder encoder = Base64.getEncoder();
            filename = "=?utf-8?B?" + encoder.encodeToString(filename.getBytes("utf-8")) + "?=";
        } else {
            // 其它瀏覽器
            filename = URLEncoder.encode(filename, "utf-8");
        }
        return filename;
    }
}

四. 項目結構

項目結構

五. 效果演示

在 Firefox 瀏覽器中訪問:
firefox

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章