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

摘要:
附件“无缓存”);PrintWriterout=响应。getWriter()//读取文件Stringpath=getServletContext()。getRealPath(“/a.txt”);charbuffer[]=新字符[256];}reader.close();

一、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文件内容:

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

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

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

结果:

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

三、下载功能源代码如下

 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);
    }

}

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

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

免责声明:文章转载自《JavaWeb学习记录(一)——response响应头之缓存设置与下载功能的实现》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇使用ast(抽象语法树)在代码中植入埋点提高机器学习模型准确率的八大方法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

[转]mysql 的日志的启动与查看

mysql有以下几种日志:错误日志:  -log-err查询日志:  -log慢查询日志: -log-slow-queries更新日志:    -log-update二进制日志:-log-bin 日志文件 文件中的信息 作用 错误日志 记录启动、运行或停止mysqld时出现的问题。 系统故障时定位故障原因 查询日志 记录建立的客户端连接和执行的...

PHP环境搭建

php环境分为两种:wanmplanmp PHP官网  w: windows 系统l: linux 系统a: apache 服务器n: nginx 服务器m: mysql 数据库p: php 服务器端的脚本语言 安装环境分为两种:集成环境 一键安装 集成环境 phpStudy wampserver apmserver xampp linux phpStu...

sass中文注释的解决方法和一些简单用法

最近用sass来编写项目中的css,发现不能添加中文注释,报错如下 于是查阅了一下发现需要在scss文件顶部加上@charset "utf-8"即可解决。 在此顺便记录一些sass的常用技巧。 注释: /* 我是注释 */ ,会保留到编译后的文件。 // 我是注释 ,只保留在SASS源文件中,编译后被省略。  /*后面加一个感叹号,表示这是...

linux总结shell

一、Shell脚本常用的头部格式: 头部的作用就是告知linux此脚本的类型; 常用的头部格式如下:(/bin/bash,是bash的路径,如果不知道路径可以通过which bash进行查看,其它命令的路径也是类似查看的) (1)#!/bin/bash:普通的linux脚本,也是最常用的,不需要交互; (2)#!/usr/bin/expect:可以自动交...

Rocksdb Compaction原理

概述      compaction主要包括两类:将内存中imutable 转储到磁盘上sst的过程称之为flush或者minor compaction;磁盘上的sst文件从低层向高层转储的过程称之为compaction或者是major compaction。对于myrocks来说,compaction过程都由后台线程触发,对于minor compacti...

Windows 8 应用商店应用开发 之 应用程序的数据存储(2)应用程序存储空间

感谢早上博客园朋友的忠告,下面我们继续Windows 8 应用商店应用开发的技术分享。想想实现了的理想,还有即将实现的理想,加油。也许是昨天的内容文字量不够的原因。今天将会提供更多的内容。 13.1应用程序的数据存储 从Windows应用商店中下载并安装应用程序到本地设备后,系统会为其分配独立的存储空间,用于存放与应用相关的文件和设置信息,并负责对其进...