您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“java web response如何提供文件下載功能”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“java web response如何提供文件下載功能”這篇文章吧。
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> <h2>資源下載:</h2> <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如何提供文件下載功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。