ServletContext对象
1、概念:代表整个web应用,可以和程序的容器(服务器)来通信
2、获取:
1、通过request对象获取
request.getServletContext();
2、通过HttpServlet获取
this.getServletContext();
@WebServlet("/servletcontextdemo1")
public class ServletContextDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、通过request对象获取
ServletContext context1 = request.getServletContext();
// 2、通过HttpServlet获取
ServletContext context2 = this.getServletContext();
System.out.println(context1);
System.out.println(context2);
System.out.println("----------------");
boolean equals = context1.equals(context2);
System.out.println(equals);
System.out.println("---------------------");
System.out.println(context1==context2);
}
3、功能:
1、获取MIME类型:
* MIME类型:就是互联网在通信过程中定义的一种文件数据类型
* 格式: 大类型/小类型 text/html image/jpeg
* 获取:String geiMimeType(String file)
@WebServlet("/servletcontextdemo2")
public class ServletContextDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1、通过HttpServlet获取
ServletContext context = this.getServletContext();
//2、定义文件名称
String file="a.jpg";
//3、获取MIME类型
String mimeType = context.getMimeType(file);
System.out.println(mimeType);
}
2、域对象:共享数据
1、setAttribute(String name,Object value);
@WebServlet("/servletcontextdemo3")
public class ServletContextDemo3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1、通过HttpServlet获取
ServletContext context = this.getServletContext();
//2、设置数据
context.setAttribute("msg","haha");
}
2、getAttribute(String name);
@WebServlet("/servletcontextdemo4")
public class ServletContextDemo4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1、通过HttpServlet获取
ServletContext context = this.getServletContext();
//2、打印数据
Object msg = context.getAttribute("msg");
System.out.println(msg);
}
3、removeAttribute(String name)
*ServletContext对象范围:所有用户所有请求的数据
3、获取文件的真实(服务器)路径
1、获取文件的真实(服务器)路径
1、方法:String getRealpath(String path);
@WebServlet("/servletcontextdemo5")
public class ServletContextDemo5 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1、通过HttpServlet获取
ServletContext context = this.getServletContext();
//2、获取文件的真实(服务器)路径
String realPath = context.getRealPath("b.txt");//web目录下资源访问
System.out.println(realPath);
String c = context.getRealPath("/WEB-INF/c.txt");//WEB-INF目录下资源访问
System.out.println(c);
String a = context.getRealPath("/src/a.txt");//注意,服务器发布后是在/WEB-INF/classes目录下
System.out.println(a);
}
文件下载案例
踩坑:content-disposition写成content-dispositiin
response.setHeader("content-disposition","attachment;filename="+filename);//固定写法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我的</title>
<!--
文件下载需求:
1、页面显示超链接
2、点击超链接后弹出下载提示框
3、完成图片下载
分析:
1、超链接指向的资源如果能够被浏览器解析,则在浏览器中展示,如果不能解析,则弹出下载框,不满足需求
2、任何资源都必须弹出下载提示框
3、使用响应头设置资源的打开方式
content-disposition:attachment;filename=xxx;
步骤:
1、定义页面,编辑超链接href属性,指向Servlet,传递资源名称filename
2、定义Sevlet
1、获取文件的名称
2、使用字节输入流加载文件进内存
3、指定response的响应头: content-disposition:attachment;filename=xxx;
4、将数据写出到response的输出流
-->
</head>
<body>
<a href="/day15_response_war_exploded/img/12b01.jpg">图片1</a>
<a href="/day15_response_war_exploded/img/12b03.mp4">视频</a>
<hr>
<a href="/day15_response_war_exploded/downloadServlet1?filename=12b02.jpg">图片1</a>
<a href="/day15_response_war_exploded/downloadServlet1?filename=12b03.mp4">视频</a>
</body>
</html>
@WebServlet("/downloadServlet1")
public class downloadservlet1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、获取请求参数,文件名称
String filename = request.getParameter("filename");
//2、使用字节输入流加载文件进内存
//2.1找到文件服务器路径
ServletContext servletContext = this.getServletContext();
String realPath = servletContext.getRealPath("/img/"+filename);
//2.2用字节流关联
FileInputStream fis = new FileInputStream(realPath);
//4、设置response的响应头
//4.1设置响应头类型:content-type
String mimeType = servletContext.getMimeType(filename);
response.setHeader("content-type",mimeType);//获取文件的mime类型
//4.2设置响应头打开方式:content-dispositiion
response.setHeader("content-disposition","attachment;filename="+filename);//固定写法
//3、将输入流的数据写到输出流中
ServletOutputStream sos = response.getOutputStream();
byte[] buff=new byte[1024 * 8];
int len=0;
while ((len = fis.read(buff)) != -1){
sos.write(buff,0,len);
}
fis.close();
}
解决中文乱码就是
@WebServlet("/downloadServlet")
public class DownloadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取请求参数,文件名称
String filename = request.getParameter("filename");
//2.使用字节输入流加载文件进内存
//2.1找到文件服务器路径
ServletContext servletContext = this.getServletContext();
String realPath = servletContext.getRealPath("/img/" + filename);
//2.2用字节流关联
FileInputStream fis = new FileInputStream(realPath);
//3.设置response的响应头
//3.1设置响应头类型:content-type
String mimeType = servletContext.getMimeType(filename);//获取文件的mime类型
response.setHeader("content-type",mimeType);
//3.2设置响应头打开方式:content-disposition
//解决中文文件名问题
//1.获取user-agent请求头、
String agent = request.getHeader("user-agent");
//2.使用工具类方法编码文件名即可
filename = DownLoadUtils.getFileName(agent, filename);
response.setHeader("content-disposition","attachment;filename="+filename);
//4.将输入流的数据写出到输出流中
ServletOutputStream sos = response.getOutputStream();
byte[] buff = new byte[1024 * 8];
int len = 0;
while((len = fis.read(buff)) != -1){
sos.write(buff,0,len);
}
fis.close();
}