java web response提供文件下载功能

时间:2023-03-09 12:55:11
java web response提供文件下载功能

*/

.hljs {
display: block;
overflow-x: auto;
padding: 0.5em;
color: #333;
background: #f8f8f8;
}

.hljs-comment,
.hljs-template_comment,
.diff .hljs-header,
.hljs-javadoc {
color: #998;
font-style: italic;
}

.hljs-keyword,
.css .rule .hljs-keyword,
.hljs-winutils,
.javascript .hljs-title,
.nginx .hljs-title,
.hljs-subst,
.hljs-request,
.hljs-status {
color: #333;
font-weight: bold;
}

.hljs-number,
.hljs-hexcolor,
.ruby .hljs-constant {
color: #099;
}

.hljs-string,
.hljs-tag .hljs-value,
.hljs-phpdoc,
.tex .hljs-formula {
color: #d14;
}

.hljs-title,
.hljs-id,
.coffeescript .hljs-params,
.scss .hljs-preprocessor {
color: #900;
font-weight: bold;
}

.javascript .hljs-title,
.lisp .hljs-title,
.clojure .hljs-title,
.hljs-subst {
font-weight: normal;
}

.hljs-class .hljs-title,
.haskell .hljs-type,
.vhdl .hljs-literal,
.tex .hljs-command {
color: #458;
font-weight: bold;
}

.hljs-tag,
.hljs-tag .hljs-title,
.hljs-rules .hljs-property,
.django .hljs-tag .hljs-keyword {
color: #000080;
font-weight: normal;
}

.hljs-attribute,
.hljs-variable,
.lisp .hljs-body {
color: #008080;
}

.hljs-regexp {
color: #009926;
}

.hljs-symbol,
.ruby .hljs-symbol .hljs-string,
.lisp .hljs-keyword,
.tex .hljs-special,
.hljs-prompt {
color: #990073;
}

.hljs-built_in,
.lisp .hljs-title,
.clojure .hljs-built_in {
color: #0086b3;
}

.hljs-preprocessor,
.hljs-pragma,
.hljs-pi,
.hljs-doctype,
.hljs-shebang,
.hljs-cdata {
color: #999;
font-weight: bold;
}

.hljs-deletion {
background: #fdd;
}

.hljs-addition {
background: #dfd;
}

.diff .hljs-change {
background: #0086b3;
}

.hljs-chunk {
color: #aaa;
}

#container {
padding: 15px;
}
pre {
border: 1px solid #ccc;
border-radius: 4px;
display: block;
background-color: #f8f8f8;
}
pre code {
white-space: pre-wrap;
}
.hljs,
code {
font-family: Monaco, Menlo, Consolas, 'Courier New', monospace;
}
:not(pre) > code {
padding: 2px 4px;
font-size: 90%;
color: #c7254e;
background-color: #f9f2f4;
white-space: nowrap;
border-radius: 4px;
}
-->

webapp项目的结构如下图:

java web response提供文件下载功能

download.html文件的内容如下:

<!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" >a.mp3</a><br>
<a href="/WEB/resource/a.exe" >a.exe</a><br>
<a href="/WEB/resource/a.txt" >a.txt</a><br>
<a href="/WEB/resource/a.xlsx" >a.xlsx</a><br>
<a href="/WEB/resource/a.png" >a.png</a><br> <p>因此,使用a标签结合servlet的response指示浏览器不解析这些待下载文件</p>
<a href="/WEB/download?filename=a.mp3" >a.mp3</a><br>
<a href="/WEB/download?filename=a.exe" >a.exe</a><br>
<a href="/WEB/download?filename=a.txt" >a.txt</a><br>
<a href="/WEB/download?filename=a.xlsx" >a.xlsx</a><br>
<a href="/WEB/download?filename=a.png" >a.png</a><br>
</body>
</html>

负责处理下载的Servlet——download.java文件的内容如下:

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.获取文件的文件系统路径
String filePath = request.getServletContext().getRealPath("resource/"+filename); //3.设置响应头,提示浏览器不要解析响应的文件数据,而是以附件(attachment)的形式解析,即下载功能
response.setContentType(this.getServletContext().getMimeType(filename));
response.setHeader("Content-Disposition", "attachment;filename="+filename); //4.读取文件的 输入流,以及响应的输出流,并将数据输出给客户端
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

注:若您觉得这篇文章还不错请点击右下角推荐,您的支持能激发作者更大的写作热情,非常感谢!