文件下载 Response(狂神JavaWeb笔记)
下载文件思路
1、获取下载文件的路径
2、下载的文件名
3、设置想办法让浏览器能够支持下载我们需要的东西
4、获取下载文件的输入流
5、创建缓冲区
6、获取OutputStream对象
7、将FileOutStream流写入到buffer缓冲区
8、使用OutputStream将缓冲区中的数据输出到客户端
实现过程
1、在idea中使用maven创建新的JavaWeb项目
2、代码实现
配置 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Servlet03</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>Servlet03 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<finalName>Servlet03</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
servlet
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
public class FileServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1.获取下载文件的路径
String realPath = ("C:\\Code\\JavaWeb\\Servlet_03\\src\\main\\resources\\db");
System.out.println("下载的文件路径为:" + realPath);
// 2.下载的文件名
String Filename = realPath.substring(realPath.lastIndexOf("\\") + 1);
// 3.设置浏览器能够支持下载我们需要的东西
resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(Filename, "utf-8"));
// 4.获取下载文件输入流
FileInputStream fileInputStream = new FileInputStream(realPath);
// 5.创建缓冲区
int length = 0;
byte[] buffer = new byte[1024];
// 6.获取OutputStream流
ServletOutputStream outputStream = resp.getOutputStream();
// 7.将获取的FileOutputStream流写入到缓冲区
while ((length = fileInputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
fileInputStream.close();
outputStream.close();
// 8.使用缓冲区的数据写到本地客户端
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
配置xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<!--配置servlet -->
<servlet>
<servlet-name>file</servlet-name>
<servlet-class>com.dmsd.servlet.FileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>file</servlet-name>
<url-pattern>/down</url-pattern>
</servlet-mapping>
</web-app>
测试
发布启动tomcat后地址栏中走down请求即可下载。