HttpServlet实现下载文件,重要的设置header,否则浏览器无法解析为下载。
Header示例:
1:response.setContentType("application/pdf");//说明实体主体内容的媒体类型
2:response.addHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(file.getName(), "UTF-8"));//文件解析为下载还是内嵌显示
3:response.setHeader("Content-Length", String.valueOf(length)); //表示实体主体部分的大小
如下示例:
package com.daxin; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class DownLoadFile
*/
public class DownLoadFile extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public DownLoadFile() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
File file = new File("C:\\Coding\\servlet_fie_upload\\aa.flv");
if (file.exists()) {
FileInputStream in = new FileInputStream(file);
// 1:设置http header信息为pdf解析方式
// response.setContentType("application/pdf"); // 1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setContentType("multipart/form-data"); //相对比较通用 response.addHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
response.setHeader("Content-Length", String.valueOf(in.available()));
// 2:
BufferedInputStream br = null;
BufferedOutputStream bw = null;
try {
br = new BufferedInputStream(in);
bw = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[1024];
int bufferSize = 0;
while (-1 != (bufferSize = br.read(buffer))) {
bw.write(buffer, 0, bufferSize);
} } catch (Exception e) {
e.printStackTrace();
} finally {
if (bw != null)
bw.close();
if (in != null)
in.close();
if (br != null)
br.close();
} } } /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }
1: Web开发中下载文件时,我们一般设置Content-Disposition告诉浏览器下载文件的名称,是否在浏览器中内嵌显示. Content-disposition: inline; filename=foobar.pdf表示浏览器内嵌显示一个文件
2:Content-disposition: attachment; filename=foobar.pdf表示会下载文件
前端页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<base href="<%=basePath%>">
<title>Insert title here</title>
</head>
<body>
<a href="DownLoadFile">下载文件</a>
</body>
</html>