如何在webstart中使用Java下载缓存?

时间:2010-07-22 10:54:21

标签: java caching java-web-start

我的Java Webstart应用程序下载了一些大型资源文件。我用:

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

但是下次启动时会再次下载资源。这些文件也不会出现在临时Internet文件的资源列表中。

在较旧的Java版本中,这有用。知道如何在当前版本中使用此缓存吗?

1 个答案:

答案 0 :(得分:0)

不是旧的Java版本,它有工作。它适用于Java Applets。以下是如何为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;
    }

}