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. 有趣的代码: fixTypeof

    typeof 可以匹配对象的类型,但是他的能力很弱,比如 typeof new String('123')会显示的object这是我们不想看到的结果很久以前JQ的作者通过Object.prototyp ...

  2. phpstudy 出现“请自行检查是否安装VC9运行库??”问题 -- 缺少vc9库

    phpStudy是一款PHP调试环境的程序集成包,该程序包集成最新的Apache+PHP+MySQL+phpMyAdmin+ZendOptimizer,一次性安装,无须配置即可使用,是非常方便.好用的 ...

  3. oracle,sqlserver同一个表两个字段和成一个列查询 (详询请加qq:2085920154)

    sql 同一张表开两个字段合成一个字段 例如   SQL tab 字段1 字段2 字段3 3 4 莫 合成 tab 字段1 字段2 3-4 莫     SQL SERVER select 字段1 + ...

  4. EXCEL文件格式不匹配,或者已经损坏,除非信任来源

    修改注册表解决: .打开注册表编辑器 方法:开始 -> 运行 -> 输入regedit -> 确定 .找到注册表子项 HKEY_CURRENT_USER\Software\Micro ...

  5. RSpec shared examples with template methods

    It’s pretty common to have multiple tests that are nearly the same. In one app, we support bidding o ...

  6. 程序猿,千万别说你不了解Docker!

    放在两年前,你不认识Docker情有可原.但如果现在你还这么说,不好意思,我只能说你OUT了.你最好马上get起来,因为有可能你们公司很快就会引入Docker. 今天就和大家讨论讨论这个备受好评的应用 ...

  7. 简单跳转到微信分享,基于libWeiChatSDK 和简单的自定义UIActivityViewController

    一.自定义UIActivity: 如果想要自定义UIActivity必须知道UIActivityViewController.首先这个类主要是用于接受字符串,RUL类型和图片类型的数据用于分享和操作的 ...

  8. html5表单验证

    表单验证为终端用户检测无效的数据并标记这些错误,是一种用户体验的优化. 下面展现浏览器自带的验证功能也可在移动端中查看: HTML部分: <!DOCTYPE html> <html ...

  9. $(function(){})、$(document).ready(function(){})....../ ready和onload的区别

    1.window.onload 当一个文档完全下载到浏览器中时,会触发 window.onload 事件. 这意味着页面上的全部元素对 javascript 而言都是可以访问的,这种情况对编写功能性的 ...

  10. 站点图标favicon.ico

    favicon.ico图标: 网站的favicon.ico需要一次额外的http请求,无论你是否有在html里面添加 link链接 <link rel="shortcut icon&q ...