2010-07-22 70 views
0

我的Java Webstart應用程序下載一些大型資源文件。我用:如何通過webstart使用Java下載緩存?

URL url = new URL("http://....."); 
URLConnection uc = url.openConnection(); 
uc.setUseCaches(true); 
uc.getInputStream(); 

但是在下次啓動時再次下載資源。這些文件也不在臨時Internet文件的資源列表中。

在較早的Java版本中,這有效。任何想法如何使用當前版本的緩存?

回答

0

不適用於舊版本的Java版本。它與Java小程序一起工作。以下是您如何爲JNLP啓用Java緩存的解決方案。您需要在代碼中調用一次JnlpResponseCache.init()以啓用它。

class JnlpResponseCache extends ResponseCache { 
    private final DownloadService service; 

    private JnlpResponseCache(){ 
     try { 
      service = (DownloadService)ServiceManager.lookup("javax.jnlp.DownloadService"); 
     } catch(UnavailableServiceException ex) { 
      throw new NoClassDefFoundError(ex.toString()); 
     } 
    } 

    static void init(){ 
     if(ResponseCache.getDefault() == null){ 
      ResponseCache.setDefault(new JnlpResponseCache()); 
     } 
    } 

    @Override 
    public CacheResponse get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders) throws IOException { 
     return null; 
    } 

    @Override 
    public CacheRequest put(URI uri, URLConnection conn) throws IOException { 
     URL url = uri.toURL(); 
     service.loadResource(url, null, service.getDefaultProgressWindow()); 
     return null; 
    } 

}