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

}

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

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

  1. JavaWeb学习记录(二十三)——文件上传与下载

    一.导入jar包

  2. HTTP学习记录:四、头信息(请求和响应)

    学习资源主要为:@小坦克HTTP相关博客 一.请求头信息(Request Header) 请求头信息包含比较多,如下: 1.Cache头域 if-modified-Since 作用:把浏览器端缓存页面 ...

  3. javaweb笔记6多个响应头以及 HttpServletResponse对象

    1 常见的响应头 Location: http://www.it315.org/index.jsp     重定向的地址.配合302的状态码一起使用,实现重定向效果. Content-Type: te ...

  4. JavaWeb学习记录总结(二十九)--Servlet\Session\Cookie\Filter实现自动登录和记住密码

    一.Servlet package autologin.servlet.login; import java.io.IOException;import java.security.MessageDi ...

  5. JavaWeb学习记录(七)——MVC操作数据库增删改查与分页功能

    一.分页工具类 package blank.util;import java.util.List; import org.springframework.jdbc.core.JdbcTemplate; ...

  6. JavaWeb学习记录(九)——Cookie的增加、删除、查看

    一.servlet功能代码: public void doGet(HttpServletRequest request, HttpServletResponse response)           ...

  7. JavaWeb学习记录(二十六)——在线人数统计HttpSessionListener监听实现

    一.session销毁控制层代码 public class InvalidateSession extends HttpServlet { public void doGet(HttpServletR ...

  8. javaweb学习记录(1)

    Java基础学习笔录 1.运行java程序,出现bad version number in.class file 编译器()的版本号高于运行环境(jre)的版本号,可以降低编译器版本号,也可以通过提升 ...

  9. JavaWeb学习记录(六)——用户登录功能

    使用JDBC.spring框架.servlet实现一个简单的用户登录功能. 一.mySql数据库 SET FOREIGN_KEY_CHECKS=0; -- ---------------------- ...

随机推荐

  1. JSP初识

    JSP最终会变成一个完整的servlet在web应用中运行.它与其他的servlet非常相似,只不过这个servlet类会由容器编写. 1.JSP的生命周期 如果一个web应用包含JSP,部署这个应用 ...

  2. hashmap and hashtable

    ①继承不同. public class Hashtable extends Dictionary implements Map public class HashMap extends Abstrac ...

  3. [windows驱动]基本概念

    https://msdn.microsoft.com/zh-cn/library/windows/hardware/ff554721 1.设备节点和设备堆栈 在windows中,设备通过即插即用设备树 ...

  4. Android-LogCat日志工具(一)

    LogCat : Android中一个命令行工具,可以用于得到程序的log信息. 就像你知道一个人的日志.航程,你可以无时无刻知道一个人在干什么. 而LogCat , 就是程序的日志.通过日志,你可以 ...

  5. 日志基本概念/rSyslog

    日志是纯文本的,在var/log/ linux系统下的日志类型: 内核信息   服务信息    应用程序信息

  6. Hash(哈希)

    一.基本概念 Hash,一般翻译做"散列",也有直接音译为"哈希"的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的 ...

  7. Matlab单一变量曲线拟合-cftool

    2.启动曲线拟合工具箱>cftool 3.进入曲线拟合工具箱界面“Curve Fitting tool”(1)点击“Data”按钮,弹出“Data”窗口:(2)利用X data和Y data的下 ...

  8. (转)innerHTML、innerText和outerHTML、outerText的区别

    原文:http://walsh.iteye.com/blog/261966 innerHTML.innerText和outerHTML.outerText的区别          博客分类: CSS/ ...

  9. C/C++整数除法以及保留小数位的问题

    题目描述 Given two postive integers A and B,  please calculate the maximum integer C that C*B≤A, and the ...

  10. error: Apostrophe not preceded by \

    解决方案为:在编译出错提示中找到相关的string.xml文档,在string标签中的字符串含有单引号(')前面,加上反斜杠(\)转义即可.