JavaWeb学习记录(一)——response响应头之缓存设置与下载功能的实现

一、HTTP中常用响应头

  • Location: http://www.it315.org/index.jsp
  • Server:apache tomcat
  • Content-Encoding: gzip
  • Content-Length: 80
  • Content-Language: zh-cn
  • Content-Type: text/html; charset=GB2312
  • Last-Modified: Tue, 11 Jul 2000 18:23:51 GMT
  • Refresh: 1;url=http://www.it315.org
  • Content-Disposition: attachment; filename=aaa.zip
  • Transfer-Encoding: chunked 
  • Set-Cookie:SS=Q0=5Lb_nQ; path=/search
  • ETag: W/"7777-1242234904000"
  • Expires: -1
  • Cache-Control: no-cache 
  • Pragma: no-cache  
  • Connection: close/Keep-Alive  
  • Date: Tue, 11 Jul 2000 18:23:51 GMT

二、设置缓存信息

  public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("-----------CacheServlet-------------");
        // 设置相应头信息
        // 设置缓存时间100秒
        // response.setDateHeader("Expires",
        // System.currentTimeMillis()+100*1000);
        // 禁止使用缓存
        // response.setDateHeader("Expires", 0);
        // response.setHeader("Cache-Control", "no-cache");
        // response.setHeader("Pragma", "no-cache");

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        // 读取文件
        String path = getServletContext().getRealPath("/a.txt");
        FileReader reader = new FileReader(new File(path));
        char buffer[] = new char[256];
        int len = 0;
        while ((len = reader.read(buffer)) != -1) {
            out.println(new String(buffer, 0, len));
        }
        reader.close();
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

    /**
     * 最后一次修改的时间
     */
    @Override
    protected long getLastModified(HttpServletRequest req) {
        String path = getServletContext().getRealPath("/a.txt");
        File file = new File(path);
        return file.lastModified();
    }

a.txt文件内容:

a.txt在项目中的放置地址:

结果:

三、下载功能源代码如下

 public class DownServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String path = request.getServletContext().getRealPath("/down/中国.png");
        File file = new File(path);
        // 下载的方式打开此操作(指定编码方式,下载文件名与源文件一致)
        response.addHeader("Content-Disposition", "attachment;fileName="
                + URLEncoder.encode(file.getName(), "UTF-8"));
        OutputStream os = response.getOutputStream();
        InputStream is = new FileInputStream(file);
        int len = 0;
        byte[] buffer = new byte[1024];
        while ((len = is.read(buffer)) != -1) {
            os.write(buffer, 0, len);
        }
        is.close();
        os.close();
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

本程序中下载文件的地址放置在该项目的如下位置:

原文地址:https://www.cnblogs.com/ly-radiata/p/4344712.html