Servlet实现文件下载
一. servlet项目创建
1. 新建maven项目
-
新建maven项目
- 记得更换jdk版本
-
项目设置
- 在模块那添加web
-
编译配置
- 添加本地tomcat
-
运行
- 如果有创建index.jsp文件的话默认打开,没有的话会报错500,没资源
2. 添加servlet包
-
配置pom.xml文件
-
<dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> </dependency> </dependencies>
-
<build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build>
-
记得关闭tomcat,不然依赖源不会实时更新,导入后长这样!
3. 新建java类
- 拓展类,自动导包
package com.yorha.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ResponseServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
二. 资源路径的获取和使用
1. 获取下载路径
String realPath = this.getServletContext().getRealPath("/WEB-INF/classes/路标.png");
2. 下载文件名
String fileName = realPath.substring(realPath.lastIndexOf("\\")+1);
3. 浏览器提供支持
resp.setHeader("Content-disposition","attachment;filename=" + URLEncoder.encode(fileName,"utf-8"));
4. 获取文件输入流
FileInputStream in = new FileInputStream(realPath);
5. 创建缓冲区
int len = 0;
byte[] buffer = new byte[1024];
6. 获取outputSteam对象
ServletOutputStream out = resp.getOutputStream();
7. 将fileOutputSteam写入缓冲区
使用OutputSteam将缓冲区内推输出到客户端
while ((len = in.read(buffer))>0){
out.write(buffer,0,len);
}
8. 用完记得关闭文件,保证安全性
in.close();
out.close();
class类完整代码
// 1. 获取下载路径
String realPath = this.getServletContext().getRealPath("/WEB-INF/classes/路标.png");
// 2. 下载文件名
System.out.println(realPath);
String fileName = realPath.substring(realPath.lastIndexOf("\\")+1);
// 3. 浏览器提供支持
resp.setHeader("Content-disposition","attachment;filename=" + URLEncoder.encode(fileName,"utf-8"));
// 4. 获取文件输入流
FileInputStream in = new FileInputStream(realPath);
// 5. 创建缓冲区
int len = 0;
byte[] buffer = new byte[1024];
// 6. 获取outputSteam对象
ServletOutputStream out = resp.getOutputStream();
// 7. 将fileOutputSteam写入缓冲区,使用OutputSteam将缓冲区内推输出到客户端
while ((len = in.read(buffer))>0){
out.write(buffer,0,len);
}
in.close();
out.close();
注意事项
使用绝对路径时报错
ava.lang.NullPointerException: Cannot invoke "String.lastIndexOf(String)" because "realPath" is null
使用/加文件名
String realPath = this.getServletContext().getRealPath("/路标.png");
时得到的路径为
D:\javaweb\response\out\artifacts\response_Web_exploded\.png
这时点开输出的资源包里面发现是没有.png文件的,所以我们需要在pom.xml文件中添加如下代码
<include>**/*.png</include>
所选段完整代码如下
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.png</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
这时就能在WEB-INF文件中看到.png文件了
然后对照sout输出的内容修改路径
String realPath = this.getServletContext().getRealPath("/WEB-INF/classes/路标.png");
此时就能正常下载了,这里注意是WEB"-"INF,不要写成WEB"_"INF,输入法按住shift是"-"是"__"
URLEncoder类
URLEncoder.encode(fileName,"utf-8")
标签:文件,String,realPath,resp,下载,servlet,response,png,out
From: https://www.cnblogs.com/yorha/p/17031857.html