JavaWeb学习记录(一)——response响应头之缓存设置与下载功能的实现
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JavaWeb学习记录(一)——response响应头之缓存设置与下载功能的实现,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2757字,纯文字阅读大概需要4分钟。
内容图文
![JavaWeb学习记录(一)——response响应头之缓存设置与下载功能的实现](/upload/InfoBanner/zyjiaocheng/1207/f3c5df5225364ce49036a2e63751e56c.jpg)
一、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);
}
}
本程序中下载文件的地址放置在该项目的如下位置:
原文:http://www.cnblogs.com/ly-radiata/p/4344712.html
内容总结
以上是互联网集市为您收集整理的JavaWeb学习记录(一)——response响应头之缓存设置与下载功能的实现全部内容,希望文章能够帮你解决JavaWeb学习记录(一)——response响应头之缓存设置与下载功能的实现所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。