• 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

  • 相关阅读:
    FileZilla
    dos2unix转换从win下vimruntime下的文件
    在 MFC SDI 程序中实现多语言程序界面
    AheadLib 2.2.150
    F982,F983班数理逻辑期末考试试题
    论文公式规范。
    Servlet/JSP配置详解
    COM沉思录(八)
    XML配置文件的读取处理
    天使和魔鬼(转载)
  • 原文地址:https://www.cnblogs.com/dehai/p/5068896.html
Copyright © 2020-2023  润新知