一、了解ServletContext对象
代表整个web应用,可以和程序的容器(服务器)来通信。
1. 概念
ServletContext是Servlet中最大的一个接口,呈现了web应用的Servlet视图。服务器会为每一个工程创建一个对象,这个对象就是ServletContext对象。这个对象全局唯一,而且工程内部的所有servlet都共享这个对象。所以叫全局应用程序共享对象。
2. 获取
1. 通过request对象获取
request.getServletContext();
2. 通过HttpServlet获取
this.getServletContext();
3. 测试
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet(name = "servletContextDemo1", value = "/servletContextDemo1")
public class servletContextDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context1 = request.getServletContext();
ServletContext context2 = this.getServletContext(); //httpServlet下的servletContext
System.out.println(context1);
System.out.println(context2);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
3. 功能
1. 获取MIME类型:
- MIME类型:在互联网通信过程中定义的一种文件数据类型
- 格式: 大类型/小类型 text/html image/jpeg
- 获取:String getMimeType(String file)
@WebServlet(name = "servletContextDemo2", value = "/servletContextDemo2")
public class servletContextDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = this.getServletContext(); //httpServlet下的servletContext
String filename = "a.jpg";
String mimeType = context.getMimeType(filename); //获取mime类型
System.out.println(mimeType); //mime类型为 image/jpeg
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
2. 域对象:共享数据
- 设置数据域:setAttribute(String name,Object value)
@WebServlet(name = "servletContextDemo3", value = "/servletContextDemo3")
public class servletContextDemo3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = this.getServletContext(); //httpServlet下的servletContext
context.setAttribute("msg","ohmyga"); //设置共享数据,给整个服务器共享
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
- 获取数据域:getAttribute(String name)
@WebServlet(name = "servletContextDemo4", value = "/servletContextDemo4")
public class servletContextDemo4 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = this.getServletContext(); //httpServlet下的servletContext
Object msg = context.getAttribute("msg");//设置共享数据
System.out.println(msg);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
二、如何获取文件真实路径
String getRealPath(String path);
1. web目录下资源访问
String a = context.getRealPath("/a.txt");
System.out.println(a);
2. WEB-INF目录下的资源访问
String b = context.getRealPath("/WEB-INF/b.txt");
System.out.println(b);
3. src目录下的资源访问
String c = context.getRealPath("/WEB-INF/classes/c.txt");
System.out.println(c);
@WebServlet(name = "servletContextDemo5", value = "/servletContextDemo5")
public class 获取文件真实路径 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = this.getServletContext(); //httpServlet下的servletContext
String a = context.getRealPath("/WEB-INF/classes/a.txt"); //src目录下的a.txt
System.out.println(a);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
三、实现文件下载
1. html文件
1. 编写一段简单的html代码,比如下载这个 1.jpg 文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>下载</title>
</head>
<body>
<a href="/Web/servletContextDemo6?filename=1.jpg">图片下载</a>
</body>
</html>
2. 预览效果
2. web 服务器代码
这里注意需要设置response响应头的mime类型和响应头的打开方式!!!
response.setHeader(String name, String value);
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.FileInputStream;
import java.io.IOException;
@WebServlet(name = "servletContextDemo6", value = "/servletContextDemo6")
public class 文件下载 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String filename = request.getParameter("filename"); //获取请求参数
//使用字节输入流加载文件进内存
ServletContext servletContext = this.getServletContext();
String realPath = servletContext.getRealPath("/WEB-INF/classes/servletContext/" + filename); //找到文件的服务器路径
FileInputStream fis = new FileInputStream(realPath); //使用字节输入流读取文件
//设置response响应头
String mimeType = servletContext.getMimeType(filename); //获取mime类型
response.setHeader("content-type",mimeType); //设置响应头类型
response.setHeader("content-disposition","attachment;filename=" + filename); //设置响应头打开方式
//将输入流的事件写出到输出事件
ServletOutputStream sos = response.getOutputStream();
byte[] buffer = new byte[1024 * 8];
int length = 0;
while((length = fis.read(buffer)) != -1){
sos.write(buffer,0,length);
}
sos.close();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
3. 预览效果
点击下载
文件就被默认(也可以用浏览器设置到其他地方)的下载到了这个位置