好記性不如爛筆頭13-使用HttpServletResponse實現檔案下載

來源:互聯網
上載者:User

標籤:httpservletresponse   web   檔案下載   


在JS中也可以實現檔案下載,但是在JS中的連結,很容易被盜鏈,而且容易造成攻擊。將檔案下載的連結放在JAVA端,能夠對使用者的請求進行更加多的校正,能防止一部分的盜鏈和攻擊。

1、使用HttpServletResponse實現下載功能的實現

  1.擷取要下載的檔案的絕對路徑

  2.擷取要下載的檔案名稱

  3.設定content-disposition回應標頭控制瀏覽器以下載的形式開啟檔案

  4.擷取要下載的檔案輸入資料流

  5.建立資料緩衝區

  6.通過response對象擷取OutputStream流

  7.將FileInputStream流寫入到buffer緩衝區

  8.使用OutputStream將緩衝區的資料輸出到用戶端瀏覽器

2、使用HttpServletResponse實現下載注意事項

檔案下載注意事項:編寫檔案下載功能時推薦使用OutputStream流,避免使用PrintWriter流,因為OutputStream流是位元組流,可以處理任意類型的資料,而PrintWriter流是字元流,只能處理字元資料,如果用字元流處理位元組資料,會導致資料丟失。

下載中文檔案時,需要注意的地方就是中文檔案名稱要使用URLEncoder.encode方法進行編碼(URLEncoder.encode(fileName, "字元編碼")),否則會出現檔案名稱亂碼。

response.setHeader("content-disposition","attachment;filename="+URLEncoder.encode(fileName,"UTF-8"));

3、HttpServletResponse實現下載的原始碼

package com.servlet;

 

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Random;

import javax.imageio.ImageIO;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

 

/**

 *通過HttpServletResponse實現簡單的檔案下載

 *

 *@author 範芳銘

 */

public class ResponseDownload extendsHttpServlet {

    publicvoid doGet(HttpServletRequest request, HttpServletResponse response)

            throwsServletException, IOException {

 

        //1.擷取要下載的檔案的絕對路徑

        StringrealPath = this.getServletContext().getRealPath("/files/fxjh.jpg");

        //2.擷取要下載的檔案名稱

        StringfileName = realPath.substring(realPath.lastIndexOf("\\") + 1);

        //3.設定content-disposition回應標頭控制瀏覽器以下載的形式開啟檔案

        response.setHeader("content-disposition","attachment;filename="

                +fileName);

        //4.擷取要下載的檔案輸入資料流

        InputStreamin = new FileInputStream(realPath);

        intlen = 0;

        //5.建立資料緩衝區

        byte[]buffer = new byte[1024];

        //6.通過response對象擷取OutputStream流

        OutputStreamout = response.getOutputStream();

        //7.將FileInputStream流寫入到buffer緩衝區

        while((len = in.read(buffer)) > 0) {

            //8.使用OutputStream將緩衝區的資料輸出到用戶端瀏覽器

            out.write(buffer,0, len);

        }

        in.close();

    }

 

    publicvoid doPost(HttpServletRequest request, HttpServletResponse response)

            throwsServletException, IOException {

        doGet(request,response);

    }

}

好記性不如爛筆頭13-使用HttpServletResponse實現檔案下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.