1.1 文件的下載概述
- 文件下載
- 服務(wù)器通過網(wǎng)絡(luò)以流的形式將文件發(fā)送到客戶端的過程
- 應(yīng)用場景
- 電影下載
- 音樂下載
- 安裝文件下載
- 實(shí)現(xiàn)方式
- 方式1
- 可以使用超鏈接來實(shí)現(xiàn)文件的下載
- <a href="要下載文件的地址" >下載</a>
- 注意:如果要下載的文件類型是瀏覽器支持的類型茬高,瀏覽器會(huì)直接打開這個(gè)文件媒峡,并不會(huì)下載這個(gè)文件织狐,比如圖片
- 方式2
- 可以使用代碼來實(shí)現(xiàn)文件的下載
- 需要有兩個(gè)頭和一個(gè)流
- Content-type 設(shè)置要下載文件的類型(MIME類型)
- Content-Disposition 通知瀏覽器呜笑,你不要關(guān)心下載文件的類型,什么類型的文件都進(jìn)行下載操作
- 字節(jié)輸出流 response.getOutputStream()
- 方式1
1.2 超鏈接方式實(shí)現(xiàn)文件下載的功能
- 好處:簡單
- 缺點(diǎn):
- 必須要是瀏覽器不能識(shí)別的格式,否則就會(huì)直接打開
- 如果資源放在web-inf路徑下面,就沒法獲取
1.3 代碼方式實(shí)現(xiàn)文件下載的功能
- 首先配置一下下載文件在項(xiàng)目中
放置下載資源.png
- 編寫一個(gè)下載頁面
<body>
<h1>超鏈接實(shí)現(xiàn)文件下載功能</h1>
<a href="${pageContext.request.contextPath}/download/pic01.jpg" >pic01.jpg</a>
<br />
<a href="${pageContext.request.contextPath}/WEB-INF/pic01.jpg" >WEB-INF/pic01.jpg</a>
<br />
<a href="${pageContext.request.contextPath}/download/pic01.zip" >pic01.zip</a>
</body>
注意:瀏覽器會(huì)根據(jù)是否能解析資源來選擇是顯示還是下載的
WEB-INF的資源瀏覽器無法訪問
- 使用代碼實(shí)現(xiàn)
也需要編寫一個(gè)下載頁面跳轉(zhuǎn)Servlet
<h1>代碼方式實(shí)現(xiàn)文件下載的功能</h1>
<a href="${pageContext.request.contextPath}/DownloadServlet" >pic01.jpg</a>
- 編寫Servlet
package com.itbear.download;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public DownloadServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//路徑
String realPath = getServletContext().getRealPath("/download");
//名稱
String fileName = "pic01.jpg";
//設(shè)置兩個(gè)頭
String mimeType = getServletContext().getMimeType(fileName);
response.setContentType(mimeType);
response.setHeader("Content-Dispostion", "attachment;filename="+fileName);
//IO流傳輸
ServletOutputStream os = response.getOutputStream();
FileInputStream fis = new FileInputStream(realPath+"/"+fileName);
byte[] bys = new byte[1024];
int len = 0;
while((len = fis.read(bys)) != -1){
os.write(bys,0,len);
}
fis.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
- 動(dòng)態(tài)獲取代碼
URL是可以 進(jìn)行拼接 提交參數(shù)數(shù)據(jù)的
<h1>代碼方式實(shí)現(xiàn)文件下載的功能</h1>
<a href="${pageContext.request.contextPath}/DownloadServlet?path=download&fileName=pic01.jpg" >pic01.jpg</a>
<br />
<a href="${pageContext.request.contextPath}/DownloadServlet?path=download&fileName=pic01.zip" >pic01.zip</a>
<br />
提交path 和 fileName
- 改寫Servelt
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//路徑
String path = request.getParameter("path");
String realPath = getServletContext().getRealPath("/" + path);
//名稱
//String fileName = "pic01.jpg";
String fileName = request.getParameter("fileName")
//設(shè)置兩個(gè)頭
//設(shè)置Content-type
String mimeType = getServletContext().g-etMimeType(fileName);
response.setContentType(mimeType);
//設(shè)置Content-Disposition
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName,"UTF-8"));
//獲取輸出流
OutputStream os = response.getOutputStream();
//創(chuàng)建輸入流
InputStream is = new FileInputStream(realPath + "/" + fileName);
byte[] bys = new byte[1024];
int len = 0;
while((len = is.read(bys)) != -1) {
os.write(bys,0,len);
}
is.close();
}
- 亂碼問題
<a href="${pageContext.request.contextPath}/DownloadServlet?path=download&fileName=小熊先生.jpg" >小熊先生.jpg</a>
這樣的傳入中文會(huì)導(dǎo)致亂碼
需要進(jìn)行重新設(shè)置編碼
String fileName = new String(request.getParameter("fileName").getBytes("ISO-8859-1"),"UTF-8");