JavaWeb_文件上传案例_文件下载
文件上传案例_文件下载
- 1.文件的下载
1)步骤:
①设置 contentType 响应头:设置响应的类型是什么?通知浏览器是个下载的文件。response.setContentType(“application/x-msdownload”);
②设置 Content-Disposition 响应头:通知浏览器不再有浏览器来自行处理(或打开)要下载的文件,而由用户手工完成。response.setHeader(“Content-Disposition”, “attachment;filename=abc.txt”);
③具体的文件:可以调用 response.getOutputStream 的方式,以 IO 流的方式发送给客户端。
OutputStream out = response.getOutputStream();
String pptFileName = "C:\\Users\\XS\\Desktop\\其他\\笔记\\JavaWeb问题笔记.txt";
InputStream in = new FileInputStream(pptFileName);
byte [] buffer = new byte[1024];
int len = 0;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
in.close();
下列文件均在WebContent目录下
download.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 静态下载 -->
<a href="xyz.txt">Download xyz.txt</a>
<br>
<br>
<!-- 动态下载 -->
<a href="test.jsp">Download test.jsp</a>
<br>
<br>
<a href="downloadServlet">Download abcd.pptx</a>
</body>
</html>
test.jsp
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//1.通知客户端浏览器:这是一个需要下载的文件,不能再按普通的html方式打开。
//即设置一个响应的类型: application/x-msdownload
response.setContentType("application/x-msdownload");
//2.通知客户端浏览器:不再由浏览器来处理该文件,而是交由用户自行处理。
//设置用户处理的方式:响应头。
response.setHeader("Content-Disposition", "attachment;filename=abc.txt");
%>
<h4>Test Page</h4>
Time:<%= new Date() %>
</body>
</html>
DownloadServlet.java
package com.xs.download.app.servlet;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/downloadServlet")
public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public DownloadServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/x-msdownload");
String fileName = "文件下载.pptx";
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName,"UTF-8"));
OutputStream out = response.getOutputStream();
String pptFileName = "C:\\Users\\XS\\Desktop\\其他\\笔记\\JavaWeb问题笔记.txt";
InputStream in = new FileInputStream(pptFileName);
byte [] buffer = new byte[1024];
int len = 0;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
in.close();
}
}
xyz.txt
ABCD
- 2.文件下载的需求:
1)在文件上传成功后的 success.jsp 页面上提供一个 “下载资源” 的超链接。
2)点击 “下载资源” 的超链接,会把请求发送到 Servlet,读取数据库,在页面上显示可以下载的资源信息。
FileName: AA.pptx
Desc: AA
下载
FileName: BB.pptx
Desc: BB
下载
3)再点击下载,即可完成对应文件的下载。