java web response提供文件下載功能的實(shí)例講解
webapp項(xiàng)目的結(jié)構(gòu)如下圖:

download.html文件的內(nèi)容如下:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>資源下載:</h1> <p> 單純地使用a標(biāo)簽時(shí),只有瀏覽器不能解析的文件才會(huì)是下載,否則將被瀏覽器直接解析。</p> <a href="/WEB/resource/a.mp3" rel="external nofollow" >a.mp3</a><br> <a href="/WEB/resource/a.exe" rel="external nofollow" >a.exe</a><br> <a href="/WEB/resource/a.txt" rel="external nofollow" >a.txt</a><br> <a href="/WEB/resource/a.xlsx" rel="external nofollow" >a.xlsx</a><br> <a href="/WEB/resource/a.png" rel="external nofollow" >a.png</a><br> <p>因此,使用a標(biāo)簽結(jié)合servlet的response指示瀏覽器不解析這些待下載文件</p> <a href="/WEB/download?filename=a.mp3" rel="external nofollow" >a.mp3</a><br> <a href="/WEB/download?filename=a.exe" rel="external nofollow" >a.exe</a><br> <a href="/WEB/download?filename=a.txt" rel="external nofollow" >a.txt</a><br> <a href="/WEB/download?filename=a.xlsx" rel="external nofollow" >a.xlsx</a><br> <a href="/WEB/download?filename=a.png" rel="external nofollow" >a.png</a><br> </body> </html>
負(fù)責(zé)處理下載的Servlet——download.java文件的內(nèi)容如下:
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.獲取請(qǐng)求下載的文件名
String filename = request.getParameter("filename");
//2.獲取文件的文件系統(tǒng)路徑
String filePath = request.getServletContext().getRealPath("resource/"+filename);
//3.設(shè)置響應(yīng)頭,提示瀏覽器不要解析響應(yīng)的文件數(shù)據(jù),而是以附件(attachment)的形式解析,即下載功能
response.setContentType(this.getServletContext().getMimeType(filename));
response.setHeader("Content-Disposition", "attachment;filename="+filename);
//4.讀取文件的 輸入流,以及響應(yīng)的輸出流,并將數(shù)據(jù)輸出給客戶端
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。
以上這篇java web response提供文件下載功能的實(shí)例講解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- JavaWeb response完成重定向?qū)崿F(xiàn)過程詳解
- java HttpServletRequest和HttpServletResponse詳解
- java 獲取request中的請(qǐng)求參數(shù)代碼詳解
- java request.getHeader("user-agent")獲取瀏覽器信息的方法
- Java service層獲取HttpServletRequest工具類的方法
- java通過HttpServletRequest獲取post請(qǐng)求中的body內(nèi)容的方法
- java 獲取HttpRequest Header的幾種方法(必看篇)
- JavaWeb response和request對(duì)象原理及實(shí)例解析
相關(guān)文章
java中構(gòu)造器內(nèi)部調(diào)用構(gòu)造器實(shí)例詳解
在本篇文章里小編給大家分享的是關(guān)于java中構(gòu)造器內(nèi)部調(diào)用構(gòu)造器實(shí)例內(nèi)容,需要的朋友們可以學(xué)習(xí)下。2020-05-05
Mybatis 實(shí)現(xiàn)一個(gè)搜索框?qū)Χ鄠€(gè)字段進(jìn)行模糊查詢
這篇文章主要介紹了Mybatis 實(shí)現(xiàn)一個(gè)搜索框?qū)Χ鄠€(gè)字段進(jìn)行模糊查詢,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
Springboot任務(wù)之異步任務(wù)的使用詳解
今天學(xué)習(xí)了一個(gè)新技能SpringBoot實(shí)現(xiàn)異步任務(wù),所以特地整理了本篇文章,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
JAVA設(shè)計(jì)模式之調(diào)停者模式詳解
這篇文章主要介紹了JAVA設(shè)計(jì)模式之調(diào)停者模式詳解,調(diào)停者模式是對(duì)象的行為模式,調(diào)停者模式包裝了一系列對(duì)象相互作用的方式,使得這些對(duì)象不必相互明顯引用,從而使它們可以較松散地耦合,需要的朋友可以參考下2015-04-04
springcloud?nacos動(dòng)態(tài)線程池Dynamic?tp配置接入實(shí)戰(zhàn)詳解
這篇文章主要為大家介紹了springcloud?nacos動(dòng)態(tài)線程池Dynamic?tp配置接入實(shí)戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Java使用JDBC連接數(shù)據(jù)庫(kù)的詳細(xì)步驟
本文詳細(xì)講解了Java使用JDBC連接數(shù)據(jù)庫(kù)的詳細(xì)步驟,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01

