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重定向 ...
随机推荐
- js+cookie 购物车
$(function () { //var ctx = new Ch(); //ctx.Clear(); //$.cookie(ctx.cookieName, ""); //ale ...
- C++中数据对齐
大体看了看数据对齐,不知道是否正确,总结如下: struct A { char name; double dHeight; int age; }; sizeof(A) = (1+7+8+4+4) = ...
- SQL 存储过程优化经验
经现场同事反映,他们用的好好的XML 导出工具最近一直报错,经常报数据库连接超时,查看数据库发现已经有100G 以上有空间了. 但导出数据的存储过程里面每次按时间只导1000多条数据,近理说有时间过滤 ...
- 优化openfire服务器,达到单机20万,集群50万
openfire压测概述 个月左右的测试,总算得到预定目标(3台服务器,并发50w用户在线) 测试环境搭建 压测客户端无他-tsung,尝试了windows安装perl失败后,使用centOS6.5作 ...
- CentOS下搭建SVN
1.安装svn yum -y install subversion 2.创建hydata目录 mkdir -p /var/svn/hydata 3.创建 m 目录 mkdir -p /var/svn/ ...
- 深入研究C语言 第二篇
1. 程序一: 首先我们研究如下程序: 回答如下问题: 1. 程序运行时n,a,b,c的段地址在哪个寄存器中? 全局变量的存储空间在什么段里?局部变量的存储空间在什么段了?参数在什么段里?函数的返回值 ...
- 通过反射及注解的运用获取SQL语句
import java.lang.reflect.*; public class BeanUtil { //这是拼接查询SQL语句的方法(getDelectSQL) public static Str ...
- Spark难道比oracle性能还差?百万级数据测试性能
本人对大数据方面也是刚刚研究,由于工作需要在实时查询与统计的性能方面要深入学习.现测试性能如下: 环境:VirtualBox host-only ubuntu版本: Linux master 4 ...
- Android多线程机制和Handler的使用
参考教程:iMooc关于Handler,http://www.imooc.com/learn/267 参考资料:Google提供Android文档Communicating with the UI T ...
- Android Sqlite数据库相关——实现 Sqlite 数据库版本升级
继承SQLiteOpenHelper类后,实现其中的onUpgrade 方法 @Override public void onUpgrade(SQLiteDatabase db, int oldVer ...