ServletContext object
- Lifecycle (it can be seen from the lifecycle that this is a global object)
- Created when the project starts
- Destroyed when the project is closed
- Concept: represents the entire web application and can communicate with the program's container (server).
- Obtain
- Get through the request object: request.getServletContext();
- Get through HttpServlet: this.getServletContext();
- function
- Get MIME type (type of file)
- MIME type: a file data type defined in the process of Internet communication
- Format: large type / small type
- String getMimeType(String file): determine the type of the file according to its suffix
- MIME type: a file data type defined in the process of Internet communication
- Domain objects (sharing data for all users)
- setAttribute(String name,Object obj)
- getAttribute(String name)
- removeAttribute(String name)
- Get the real (server) path of the file
- File under web: getRealPath("/ filename");
- Under web/WEB-INF: getRealPath("/WEB-INF / filename");
- Under src: getRealPath("/WEB-INF/classes / filename");
- Get MIME type (type of file)
There is a problem of Chinese code scrambling when downloading files, so let's start with a tool class
package cn.itcast.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 browser filename = URLEncoder.encode(filename, "utf-8"); filename = filename.replace("+", " "); } else if (agent.contains("Firefox")) { // Firefox BASE64Encoder base64Encoder = new BASE64Encoder(); filename = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?="; } else { // Other browsers filename = URLEncoder.encode(filename, "utf-8"); } return filename; } }
Next is the implementation of servlet class:
@WebServlet(name = "DownloadServlet",urlPatterns = "/download3") public class DownloadServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 1. Get file name String filename = request.getParameter("filename"); // 2. Get the real path // 2.1 get ServletContext ServletContext servletContext = this.getServletContext(); // 2.2 select the path according to the location of file storage. My path is the file under upload under the web String realPath = servletContext.getRealPath("/upload/" + filename); // 3. Set response head // 3.1 setting content type response.setHeader("content-type",servletContext.getMimeType(filename)); // 3.2 solve Chinese code disorder String agent = request.getHeader("user-agent"); filename = DownLoadUtils.getFileName(agent,filename); // 3.3 setting content disposition response.setHeader("content-disposition","attachment;filename=" + filename); // 4. Standard IO flow FileInputStream fis = new FileInputStream(realPath); ServletOutputStream sos = response.getOutputStream(); byte[] b = new byte[1024 * 8]; int len = 0; while((len = fis.read(b)) != -1){ sos.write(b,0,len); } fis.close(); sos.close(); } }