2009-07-27 6 views

答えて

-1

私はちょうどこの同じ問題に遭遇し、自分のWebCacheクラスを投げました。まだテストしていませんが、あなたが望むならそれを試すことができます。ページをキャッシュするディレクトリで構築し、getPage(String url)を呼び出してページを取得します。 getPageはキャッシュディレクトリを最初にチェックし、存在しなければキャッシュにダウンロードして結果を返します。キャッシュファイル名はurl.hashCode()+ ".cache"

です。これはページのソースを取得するためのものですが、URLConnectionで何をしたいのか分かりませんが、助けて。

/** 
* A tool for downloading and reading the source code of HTML pages. 
* Prevents repeated downloading of pages by storing each page in a cache. 
* When it recieves a page request, it first looks in its cache. 
* If it does not have the page cached, it will download it. 
* 
* Pages are stored as <cachedir>/<hashcode>.cache 
* 
* @author Mike Turley 
*/ 

import java.io.*; 
import java.net.*; 

public class WebCache { 
    File cachedir; 
    boolean enabled; 

    /** 
    * Create a web cache in the given directory. 
    */ 
    public WebCache(File cachedir, boolean enabled) { 
     this.cachedir = cachedir; 
     this.enabled = enabled; 
    } 
    public WebCache(String cachedir, boolean enabled) { 
     this.cachedir = new File(cachedir); 
     this.enabled = enabled; 
    } 
    public WebCache(File cachedir) { 
     this.cachedir = cachedir; 
     this.enabled = true; 
    } 
    public WebCache(String cachedir) { 
     this.cachedir = new File(cachedir); 
     this.enabled = true; 
    } 

    /** 
    * Get the content for the given URL. 
    * First check the cache, then check the internet. 
    */ 
    public String getPage(String url) { 
     try { 
      if(enabled) { 
       File cachefile = new File(cachedir.getAbsolutePath() + url.hashCode() + ".cache"); 
       //FIXME - might be missing a slash between path and hashcode. 
       if(cachefile.exists()) return loadCachedPage(url); 
      } 
      return downloadPage(url); 
     } catch(Exception e) { 
      System.err.println("Problem getting page at " + url); 
      e.printStackTrace(); 
      return null; 
     } 
    } 

    public void clear() { 
     try { 
      File[] cachefiles = cachedir.listFiles(); 
      for(int i=0; i<cachefiles.length; i++) { 
       cachefiles[i].delete(); 
      } 
      cachedir.delete(); 
     } catch(Exception e) { 
      System.err.println("Problem clearing the cache!"); 
      e.printStackTrace(); 
     } 
    } 

    public String downloadPage(String url) { 
     try { 
      URL weburl = new URL(url); 
      URLConnection urlc = weburl.openConnection(); 
      urlc.setDoInput(true); 
      urlc.setDoOutput(false); 
      BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream())); 
      if(!cachedir.exists()) cachedir.mkdir(); 
      File outfile = new File(cachedir.getAbsolutePath() + url.hashCode() + ".cache"); 
      // FIXME - might be missing a slash between path and hashcode. 
      PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outfile))); 
      StringBuilder sb = new StringBuilder(""); 
      String inputline; 
      while ((inputline = in.readLine()) != null) { 
       out.println(inputline); 
       sb.append(inputline); 
      } 
      in.close(); 
      out.close(); 
      return sb.toString(); 
     } catch(Exception e) { 
      System.err.println("Problem connecting to URL " + url); 
      e.printStackTrace(); 
      return null; 
     } 
    } 

    public String loadCachedPage(String url) { 
     try { 
      File infile = new File(cachedir.getAbsolutePath() + url.hashCode() + ".cache"); 
      // FIXME - might be missing a slash between path and hashcode. 
      BufferedReader in = new BufferedReader(new FileReader(infile)); 
      StringBuilder sb = new StringBuilder(""); 
      while (in.ready()) sb.append(in.readLine()); 
      in.close(); 
      return sb.toString(); 
     } catch(Exception e) { 
      System.err.println("Problem loading cached page " + url); 
      e.printStackTrace(); 
      return null; 
     } 
    } 

    public void setEnabled(boolean enabled) { 
     this.enabled = enabled; 
    } 
} 
+0

この実装では、HTTP仕様ではいつ、どのようにリソースをキャッシュできるのか、またキャッシュする必要がないのかについては詳しく説明していません。 ステータスコード(つまり、3xx再送信、5xxサーバエラー、検証(Eタグ、最終変更)、非無効メソッド(POST/PUT/DELETE)など)の処理が欠落しています。問題の大きな山... このような不自由なキャッシングレイヤーを使用する前に、squid、ngnixなどのスタンドアロンプ​​ロキシーセットアップを使用することをお勧めします。 – ordnungswidrig

-1

このようにしないでください。キャッシングHTTPプロキシをデプロイします。たとえば、Apache Squidです。

1

URLConnectionからApache HttpClientに切り替えた場合、HttpClient Cacheを使用できます。

関連する問題