import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import my.httpClient.MD5Helper; import org.apache.http.client.cache.HttpCacheEntry;
import org.apache.http.client.cache.HttpCacheStorage;
import org.apache.http.client.cache.HttpCacheUpdateCallback;
import org.apache.http.client.cache.HttpCacheUpdateException; public class DiskCache implements HttpCacheStorage { public DiskCache() { } public HttpCacheEntry getEntry(String url) throws IOException { HttpCacheEntry entry = null; // 一个文件一个缓存项,使用 请求的url进行hash
String path = getPath(url);
// System.out.println("path:" + path);
// 判断文件是否存在
File file = new File(path);
if (file.exists() == false) {
return null;
} try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(
path));
entry = (HttpCacheEntry) in.readObject();
in.close();
// System.out.println("object read here:");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} return entry; } public void putEntry(String url, HttpCacheEntry entry) throws IOException { String path = getPath(url); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
path));
out.writeObject(entry); // System.out.println("object has been written..");
out.close(); } public void removeEntry(String url) throws IOException { String path = getPath(url); // 判断文件是否存在
File file = new File(path);
if (file.exists() == true) { file.delete();
}
} public void updateEntry(String url, HttpCacheUpdateCallback callback)
throws IOException, HttpCacheUpdateException { String path = getPath(url);
HttpCacheEntry existingEntry = null; // 判断文件是否存在,若文件存在,则取出文件的内容
File file = new File(path);
if (file.exists() == true) { ObjectInputStream in = new ObjectInputStream(new FileInputStream(
path));
try {
existingEntry = (HttpCacheEntry) in.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
in.close();
System.out.println("object read here:");
} HttpCacheEntry updatedEntry = callback.update(existingEntry); // 获取更新过的缓存实体 // 不管存在不存在,都用保存新的实体项。
putEntry(url, updatedEntry); } /*
* 根据url地址获取缓存项在文件系统中的存储地址
*/
private String getPath(String url) {
String key = MD5Helper.string2MD5(url);
String path = "c:\\http-cache\\" + key;
return path;
} }

参考资料:

http://hc.apache.org/httpcomponents-client-ga/httpclient-cache/xref/index.html

http://hc.apache.org/httpcomponents-client-ga/httpclient-cache/xref/org/apache/http/impl/client/cache/ManagedHttpCacheStorage.html

apache httpclient CacheStorage的一个自定义实现的更多相关文章

  1. Apache HttpClient 5 使用详细教程

    点赞再看,动力无限. 微信搜「程序猿阿朗 」. 本文 Github.com/niumoo/JavaNotes 和 未读代码博客 已经收录,有很多知识点和系列文章. 超文本传输协议(HTTP)可能是当今 ...

  2. 一个封装的使用Apache HttpClient进行Http请求(GET、POST、PUT等)的类。

    一个封装的使用Apache HttpClient进行Http请求(GET.POST.PUT等)的类. import com.qunar.payment.gateway.front.channel.mp ...

  3. 如何在Apache HttpClient中设置TLS版本

    1.简介 Apache HttpClient是一个底层.轻量级的客户端HTTP库,用于与HTTP服务器进行通信. 在本教程中,我们将学习如何在使用HttpClient时配置支持的传输层安全(TLS)版 ...

  4. 论httpclient上传带参数【commons-httpclient和apache httpclient区别】

    需要做一个httpclient上传,然后啪啪啪网上找资料 1.首先以前系统中用到的了commons-httpclient上传,找了资料后一顿乱改,然后测试 PostMethod filePost = ...

  5. 一个自定义 HBase Filter -“通过RowKeys来高性能获取数据”

    摘要: 大家在使用HBase和Solr搭建系统中经常遇到的一个问题就是:“我通过SOLR得到了RowKeys后,该怎样去HBase上取数据”.使用现有的Filter性能差劲,网上也没有现成的自定义Fi ...

  6. Apache HttpClient使用之阻塞陷阱

    前言: 之前做个一个数据同步的定时程序. 其内部集成了某电商的SDK(简单的Apache Httpclient4.x封装)+Spring Quartz来实现. 原本以为简单轻松, 喝杯咖啡就高枕无忧的 ...

  7. 使用Apache HttpClient 4.x发送Json数据

    Apache HttpClient是Apache提供的一个开源组件,使用HttpClient可以很方便地进行Http请求的调用.自4.1版本开始,HttpClient的API发生了较大的改变,很多方法 ...

  8. 《Apache HttpClient 4.3开发指南》

    转载自:http://blog.csdn.net/chszs/article/details/16854747 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chs ...

  9. apache 网页301重定向、自定义400/403/404/500错误页面

    首先简单介绍一下,.htaccess文件是Apache服务器中的一个配置文件(Nginx服务器没有),它负责相关目录下的网页配置.通过对.htaccess文件进行设置,可以帮我们实现:网页301重定向 ...

随机推荐

  1. odoo模块

    odoo模块包括 业务对象,web控制器,数据文件,前面2个是Python模块,而数据文件则是odoo特有的文件,例如,odoo数据文件,web数据.这些数据文件在 __odoo__.py 进行定义, ...

  2. 20145229&20145316《信息安全系统设计基础》实验一 开发环境的熟悉

    实验封面 实验内容 本次实验使用 Redhat Linux 9.0 操作系统环境,安装 ARM-Linux 的开发库及编译器.创建一个新目录,并在其中编写 hello.c 和 Makefile 文件. ...

  3. 如何用VB.Net创建一个三层的数据库应用程序

    [b]1.[/b][b]概论:[/b] 本文将介绍如何创建一个三层应用程序,并且将介绍如何创建一个Web Service服务. ADO.NET创建Windows三层结构应用程序的体系架构如下图所示: ...

  4. 1154. Easy sort

    #include<iostream>#include<cmath>#include<iomanip>#include<algorithm>using n ...

  5. ros下多机器人系统(1)

    multi-robot system 经过两个多月的ros学习,对ros的认识有了比较深入的了解,本篇博客主要记录在ros下开发多机器人系统以及对ros更深入的开发.本篇博客是假定读者已经学习完了全部 ...

  6. hadoop多次搭建后,完整总结(累死宝宝了,搭建了十多遍了)

    1.安装JDK1.1上传运用软件FileZilla,将windows上的jdk压缩包放到linux的root目录下 1.2解压jdk #创建文件夹 mkdir /usr/java(不要挂在在" ...

  7. android 的数学公式图片转换

    在应用中的数学公式是不能直接以文本显示和输入的,包括在一些学习类网站上看到的公式,他们都是以gif图片的形式展示出来的.而怎么样生成各种各样的gif图片形式的数学公式呢,此处未作深入研究,我所知道的是 ...

  8. Android广播大全

    1.String ADD_SHORTCUT_ACTION 动作:在系统中添加一个快捷方式. 2.String ALL_APPS_ACTION 动作:列举所有可用的应用.输入:无. 3.String A ...

  9. openssl证书制作详细教程

    自签名证书及验证 模拟证书涉及的角色 创建证书目录 mkdir ~/certs cd ~/certs 认证机构.网站.浏览器/用户 mkdir root web user 机构自签名证书生成和发布 生 ...

  10. iOS-延时操作汇总

    在iOS开发中,一个操作我们希望不要立刻执行,而是等上几秒之后再来处理,这时我们就需要延时处理,我们来看看这些方 1.最直接的方法performSelector:withObject:afterDel ...