java web response提供文件下載功能的實例講解
webapp項目的結(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標簽時,只有瀏覽器不能解析的文件才會是下載,否則將被瀏覽器直接解析。</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標簽結(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>
負責處理下載的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.獲取請求下載的文件名
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提供文件下載功能的實例講解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- JavaWeb response完成重定向?qū)崿F(xiàn)過程詳解
- java HttpServletRequest和HttpServletResponse詳解
- java 獲取request中的請求參數(shù)代碼詳解
- java request.getHeader("user-agent")獲取瀏覽器信息的方法
- Java service層獲取HttpServletRequest工具類的方法
- java通過HttpServletRequest獲取post請求中的body內(nèi)容的方法
- java 獲取HttpRequest Header的幾種方法(必看篇)
- JavaWeb response和request對象原理及實例解析
相關(guān)文章
java中構(gòu)造器內(nèi)部調(diào)用構(gòu)造器實例詳解
在本篇文章里小編給大家分享的是關(guān)于java中構(gòu)造器內(nèi)部調(diào)用構(gòu)造器實例內(nèi)容,需要的朋友們可以學習下。2020-05-05
Mybatis 實現(xiàn)一個搜索框?qū)Χ鄠€字段進行模糊查詢
這篇文章主要介紹了Mybatis 實現(xiàn)一個搜索框?qū)Χ鄠€字段進行模糊查詢,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
Springboot任務(wù)之異步任務(wù)的使用詳解
今天學習了一個新技能SpringBoot實現(xiàn)異步任務(wù),所以特地整理了本篇文章,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下2021-06-06
springcloud?nacos動態(tài)線程池Dynamic?tp配置接入實戰(zhàn)詳解
這篇文章主要為大家介紹了springcloud?nacos動態(tài)線程池Dynamic?tp配置接入實戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12

