HttpServletResponse
HttpServletResponse
web服务器接收到客户端的http请求,针对这个请求,分别建立了一个代表请求的HttpServletResponse对象;和一个代表响应的HttpServletRequest对象。
如果要获取客户端请求过来的参数:HttpServletRequest对象。
如果要给客户端响应一些信息:找HttpServletResponse对象。
getOutputStream()和getWriter()
1 public ServletOutputStream getOutputStream() throws IOException; 2 public PrintWriter getWriter() throws IOException;
1 public void setCharacterEncoding(String charset); 2 public void setContentLength(int len); 3 public void setContentLengthLong(long len); 4 public void setContentType(String type); 5 public void setDateHeader(String name, long date); 6 public void addDateHeader(String name, long date); 7 public void setHeader(String name, String value); 8 public void addHeader(String name, String value); 9 public void setIntHeader(String name, int value); 10 public void addIntHeader(String name, int value);
设置浏览器支持(Content-Disposition)我们需要下载的东西,且通过URLEncoder.encode编码文件夹名称,否则中文可能出现乱码
将FileOutputStream流写入到buffer缓冲区,并使用OutputStream将缓冲区的数据输入到客户端
2、修改Tomcat
3、写FileServlet类
1 package com.wang.servlet; 2 import javax.servlet.ServletException; 3 import javax.servlet.ServletOutputStream; 4 import javax.servlet.http.HttpServlet; 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 import java.io.File; 8 import java.io.FileInputStream; 9 import java.io.IOException; 10 import java.io.PrintWriter; 11 import java.net.URLEncoder; 12 public class FileServlet extends HttpServlet { 13 @Override 14 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 15 //step1:获取下载文件的路径 16 String realPath = "C:\\Users\\WangZan\\Pictures\\Saved Pictures\\王赞.jpg"; 17 System.out.println("下载文件的路径:" + realPath); 18 //step2:获取下载的文件名 19 String filename = realPath.substring(realPath.lastIndexOf("\\") + 1); 20 //step3:设置浏览器支持(Content-Disposition)我们需要下载的东西,且通过URLEncoder.encode编码文件夹名称,否则中文可能出现乱码 21 resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8")); 22 //step4:获取下载文件的输入流 23 FileInputStream in = new FileInputStream(realPath); 24 //step5:创建缓冲区 25 int len = 0; 26 byte[] buffer = new byte[1024]; 27 //step6:获取OutputStream对象 28 ServletOutputStream out = resp.getOutputStream(); 29 //step7:将FileOutputStream流写入到buffer缓冲区,并使用OutputStream将缓冲区的数据输入到客户端 30 while ((len = in.read(buffer)) > 0) { 31 out.write(buffer, 0, len); 32 } 33 //step8:关闭流 34 in.close(); 35 out.close(); 36 } 37 38 @Override 39 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 40 doGet(req, resp); 41 } 42 }
4、注册和映射
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 5 http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 6 version="4.0" 7 metadata-complete="true"> 8 <!--注册和映射--> 9 <servlet> 10 <!--这个hello的名字是随便起的,只要和下面的mapping中的名字相同就行--> 11 <servlet-name>filedown</servlet-name> 12 <servlet-class>com.wang.servlet.FileServlet</servlet-class> 13 </servlet> 14 <!--一个Servlet对应一个Mapping:映射--> 15 <servlet-mapping> 16 <servlet-name>filedown</servlet-name> 17 <url-pattern>/down</url-pattern> 18 </servlet-mapping> 19 </web-app>
5、运行