apache httpclient CacheStorage的一个自定义实现
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的一个自定义实现的更多相关文章
- Apache HttpClient 5 使用详细教程
点赞再看,动力无限. 微信搜「程序猿阿朗 」. 本文 Github.com/niumoo/JavaNotes 和 未读代码博客 已经收录,有很多知识点和系列文章. 超文本传输协议(HTTP)可能是当今 ...
- 一个封装的使用Apache HttpClient进行Http请求(GET、POST、PUT等)的类。
一个封装的使用Apache HttpClient进行Http请求(GET.POST.PUT等)的类. import com.qunar.payment.gateway.front.channel.mp ...
- 如何在Apache HttpClient中设置TLS版本
1.简介 Apache HttpClient是一个底层.轻量级的客户端HTTP库,用于与HTTP服务器进行通信. 在本教程中,我们将学习如何在使用HttpClient时配置支持的传输层安全(TLS)版 ...
- 论httpclient上传带参数【commons-httpclient和apache httpclient区别】
需要做一个httpclient上传,然后啪啪啪网上找资料 1.首先以前系统中用到的了commons-httpclient上传,找了资料后一顿乱改,然后测试 PostMethod filePost = ...
- 一个自定义 HBase Filter -“通过RowKeys来高性能获取数据”
摘要: 大家在使用HBase和Solr搭建系统中经常遇到的一个问题就是:“我通过SOLR得到了RowKeys后,该怎样去HBase上取数据”.使用现有的Filter性能差劲,网上也没有现成的自定义Fi ...
- Apache HttpClient使用之阻塞陷阱
前言: 之前做个一个数据同步的定时程序. 其内部集成了某电商的SDK(简单的Apache Httpclient4.x封装)+Spring Quartz来实现. 原本以为简单轻松, 喝杯咖啡就高枕无忧的 ...
- 使用Apache HttpClient 4.x发送Json数据
Apache HttpClient是Apache提供的一个开源组件,使用HttpClient可以很方便地进行Http请求的调用.自4.1版本开始,HttpClient的API发生了较大的改变,很多方法 ...
- 《Apache HttpClient 4.3开发指南》
转载自:http://blog.csdn.net/chszs/article/details/16854747 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chs ...
- apache 网页301重定向、自定义400/403/404/500错误页面
首先简单介绍一下,.htaccess文件是Apache服务器中的一个配置文件(Nginx服务器没有),它负责相关目录下的网页配置.通过对.htaccess文件进行设置,可以帮我们实现:网页301重定向 ...
随机推荐
- colorbox 自适应 高度
$(".example3").colorbox({ inline: true, scrolling: false , onComplete: ...
- (转)testng对失败时截图处理
写这篇微博之前,自动化测试代码中有对于失败测试截图的功能,但是却散落在各个catch语句块中.不便于以后的扩展和维护,AOP思想里说明是面向切面编程,把公共的组件提取出来,可以单独修改维护. 但是直到 ...
- iOS.ReactNative-3-about-viewmanager-uimanager-and-bridgemodule
RCTViewManager and RCTUIManager 1. RCTViewManager 1.1 RCTViewManager 实现了接口RCTBridgeModule @interface ...
- js为空的几种情况
1.null,对象不存在 var ii= document.getElementById("id"); alert(ii); 当前页面不存在id对象 2. undefined v ...
- 脉冲计数功能在ESM335x-Linux主板上的实现
1.综述 在工业控制中,经常需要获取脉冲信号计数值.频率.周期.占空比等参数.英创嵌入式主板ESM335X系列 Linux系统现已实现外部输入脉冲信号的计数.频率.周期.占空比测量功能. 主要功能及技 ...
- arch 安装图形界面
图形界面的安装.--- 引导: .显卡驱动,这里我们是集成显卡,根据wiki提示安装xf86-video-intel .安装xorg-server,xorg-server-utils,xorg-xin ...
- RGB颜色中的参数是变量的时候,为什么要加上两个+号在左右?
<script> function draw(){ var c=document.getElementById("mycanvas"); var cxt=c.getCo ...
- Android应用程序Monkey测试
Monkey是Android SDK中附带的一个测试工具:Monkey用于进行压力测试,软件开发人员结合monkey打印日志和系统日志,解决测试中出现的问题. Monkey测试的特点:所有事件都是随机 ...
- openssl证书制作详细教程
自签名证书及验证 模拟证书涉及的角色 创建证书目录 mkdir ~/certs cd ~/certs 认证机构.网站.浏览器/用户 mkdir root web user 机构自签名证书生成和发布 生 ...
- Ubuntu 14.04 配置静态IP
命令行手工配置静态IP比较麻烦,记录于此备查. 1,ubuntu的网络配置文件在: # /etc/network/interfaces //这个文件里 2,默认安装时,网络配置是使用DHCP自动分配I ...