request.getServletContext();
this.getServletContext();
//1.通过request对象获取
ServletContext context1 = request.getServletContext();
//2.通过HttpServlet获取
ServletContext context2 = this.getServletContext();
System.out.println(context1);//org.apache.catalina.core.ApplicationContextFacade@4b65ad46
System.out.println(context2);//org.apache.catalina.core.ApplicationContextFacade@4b65ad46
格式: 大类型/小类型 text/html image/jpeg
获取:String getMimeType(String file)
//1.获取HTTPServlet获取
ServletContext context = this.getServletContext();
//2.定义文件名称
String filename = "a.jpg";
//3.获取MIME类型
String mimeType = context.getMimeType(filename);
System.out.println(mimeType);//---->image/jpeg
(2)、 域对象:共享数据
①、 setAttribute(String name,Object value)
②、 getAttribute(String name)
③、 removeAttribute(String name)
ServletContext对象范围:所有用户所有请求的数据
下面的例子中,当访问ServletContextDemo3路径时,在里面存储数据;然后当我们访问ServletContextDemo4时获取数据。
@WebServlet("/ServletContextDemo3")
public class ServletContextDemo3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取HTTPServlet获取
ServletContext context = this.getServletContext();
//设置数据
context.setAttribute("msg","haha");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
@WebServlet("/ServletContextDemo4")
public class ServletContextDemo4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取HTTPServlet获取
ServletContext context = this.getServletContext();
//获取数据
Object msg = context.getAttribute("msg");
System.out.println(msg);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
(3)、 获取文件的真实(服务器)路径
String getRealPath(String path)
String b = context.getRealPath("/b.txt");//web目录下资源访问
System.out.println(b);
String c = context.getRealPath("/WEB-INF/c.txt");//WEB-INF目录下的资源访问
System.out.println(c);
String a = context.getRealPath("/WEB-INF/classes/a.txt");//src目录下的资源访问
System.out.println(a);
文件下载需求:
1. 页面显示超链接
2. 点击超链接后弹出下载提示框
3. 完成图片文件下载
分析:
1. 超链接指向的资源如果能够被浏览器解析,则在浏览器中展示,如果不能解析,则弹出下载提示框。
2. 任何资源都必须弹出下载提示框
3. 使用响应头设置资源的打开方式:content-disposition:attachment;filename=xxx
步骤:
1. 定义页面,编辑超链接href属性,指向Servlet,传递资源名称filename
2. 定义Servlet
(1)、 获取文件名称
(2)、 使用字节输入流加载文件进内存
(3)、 指定response的响应头: content-disposition:attachment;filename=xxx
(4)、 将数据写出到response输出流
<html lang="en">
<head>
<meta charset="UTF-8">
<title>downloadtitle>
head>
<body>
<a href="/LoginCase/img/1.jpg">图片浏览a>
<a href="/LoginCase/DownloadServlet?filename=1.jpg">图片下载a>
body>
html>
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
@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);//将图片保存在web/img/文件目录下
//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
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();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
问题:中文文件问题:如果按照上述的代码实现文件下载的概念,然后将文件命名为中文,这样会导致中文乱码。
解决思路:
(1)、 获取客户端使用的浏览器版本信息
(2)、 根据不同的版本信息,设置filename的编码方式不同
导入utils包里面的DownLoadUtils.java文件
package cn.itcast.web.utils;
import sun.misc.BASE64Encoder;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class DownLoadUtils {
public static String getFileName(String agent, String filename) throws UnsupportedEncodingException {
if (agent.contains("MSIE")) {
// IE浏览器
filename = URLEncoder.encode(filename, "utf-8");
filename = filename.replace("+", " ");
} else if (agent.contains("Firefox")) {
// 火狐浏览器
BASE64Encoder base64Encoder = new BASE64Encoder();
filename = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?=";
} else {
// 其它浏览器
filename = URLEncoder.encode(filename, "utf-8");
}
return filename;
}
}
然后再response.setHeader("content-disposition","attachment;filename="+filename);
这行代码前加入下面的几行代码即可
//解决中文文件名问题
//1.获取user-agent请求头
String agent = request.getHeader("user-agent");
//2.使用工具类方法编码文件名即可
filename = DownLoadUtils.getFileName(agent,filename);