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

最新文章

  1. pscp工具上传或取回文件、文件夹
  2. wc命令
  3. JS怎么动态命名变量名
  4. 使用ASP.Net WebAPI构建REST服务(一)——简单的示例
  5. svn命令在linux下的使用
  6. android通过pc脚本执行sqlite3脚本
  7. win8系统 host文件无法修改解决之道
  8. System,Integer,Calendar,Random和容器
  9. UVa 10129 (并查集 + 欧拉路径) Play on Words
  10. Java 远程通讯技术及原理分析
  11. php中传值与传引用的区别。什么时候传值什么时候传引用?
  12. 制作ado开发辅助工具类SqlHelper
  13. jquery validate form 异步提交
  14. Swift语言指南(八)--语言基础之元组
  15. 11g R2RAC Dynamic remastering
  16. 用while判读循环语句1+1/2!+1/3!+...1/20!的和阶乘的计算方法 式:n!=n*(n-1)!
  17. 《单元测试之道Java版》的读书笔记
  18. TextInputLayout 用法
  19. 不一样的ssm
  20. 第一次作业——WorkCount

热门文章

  1. 查看Oracle中是否有锁表的sql
  2. 等宽字体延伸到的 ch 长度单位和动画 animation-timing-function
  3. NC JDK报tools.jar错误(61版本)
  4. HTML可编辑的select
  5. 新手看Mockplus
  6. AndroidLinker与SO加壳技术之上篇
  7. Android视图状态及重绘流程分析,带你一步步深入了解View(三)
  8. python入门简介
  9. C# 加密
  10. zabbix server配置文件