JavaEE實現(xiàn)文件下載
我們先來看一個最簡單的文件下載的例子:
package com.yyz.response;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//文件下載
public class ResponseDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String realpath = this.getServletContext().getRealPath("/download/1.gif");
String filename = realpath.substring(realpath.lastIndexOf("\\")+1);
response.setHeader("content-disposition", "attachment;filename="+filename);
//服務(wù)器通過這個頭,告訴瀏覽器以下載方式打開數(shù)據(jù)
FileInputStream in = new FileInputStream(realpath);
int len = 0;
byte buffer[]=new byte[1024];
OutputStream out = response.getOutputStream();
while((len = in.read(buffer))>0){
out.write(buffer, 0, len);
}
in.close();
//out不用close,response在銷毀的時候服務(wù)器會自動關(guān)閉與response相關(guān)的流。
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
該段代碼的功能是從服務(wù)器端下載圖片1.png。目錄結(jié)構(gòu)用MyEclipse的package explorer顯示如下:
讓我們增加一點難度,我們要下載的文件是一個中文名字的文件。由于在http協(xié)議中頭文件中的東西只能是ASCII字符,因而通過上述方式(直接將 String realpath = this.getServletContext().getRealPath("/download/1.gif");改為 String realpath = this.getServletContext().getRealPath("/download/圖片.gif");)直接拿文件,
會出現(xiàn)亂碼問題。附上測試結(jié)果:
要解決這個問題,要用到 URLEncoder類的encode方法:
package com.yyz.response;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//中文文件下載時,中文文件名要經(jīng)過URL編碼。
public class ResponseDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String realpath = this.getServletContext().getRealPath("/download/圖片.gif");
String filename = realpath.substring(realpath.lastIndexOf("\\")+1);
response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(filename,"UTF-8"));
//本函數(shù)將字符串以 URL 編碼
FileInputStream in = new FileInputStream(realpath);
int len = 0;
byte buffer[]=new byte[1024];
OutputStream out = response.getOutputStream();
while((len = in.read(buffer))>0){
out.write(buffer, 0, len);
}
in.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
附上測試結(jié)果:
另外這里有一個小細節(jié)需要大家注意:
不能用FileReader代替FileInputStream。用FileReader會丟失數(shù)據(jù),原因是這樣的:FileReader是字符流,而圖片,媒體文件等的數(shù)據(jù)都是以01的方式存儲,用FileReader讀的時候需要查閱一個編碼表,如果未指定一種編碼,則使用相應(yīng)平臺的默認編碼。如在中國的電腦就會去查GB2312。當(dāng)讀到GB2312碼表中不存在的編碼時,會將該數(shù)據(jù)編碼成'?',結(jié)束后數(shù)據(jù)就變成中文和'?'的混合。發(fā)到客戶端后顯示時再次查閱碼表,將所有的'?'替換成'?'的編碼,就會丟失數(shù)據(jù)。向這種細節(jié)只需要記住一點:字節(jié)流可以處理任意類型的數(shù)據(jù),字符流只能處理字符數(shù)據(jù)。
相關(guān)文章
學(xué)習(xí)Java HashMap,看這篇就夠了
這篇文章主要介紹了Java HashMap的相關(guān)資料,文中示例代碼非常詳細,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07