防止应用程序服务器上的缓存.svg映像

时间:2017-01-30 20:23:37

标签: apache caching svg batik

我有一个JWS应用程序可以缓存几种不同的资源类型。但是,我不想缓存.svg图像。似乎框架不支持我设置的服务器端缓存控制HTTP标头。

我想知道是否还有其他方法可以在没有缓存的情况下加载.svg图像。我愿意在我的loadSVGDocument()方法中提供一个解决方案,但我的代码目前是围绕Apache Batik构建的,用于加载.svg文件。是否有解决办法在Batik库中传递带有noCache标志的InputStream,类似于下面DocumentBuilderFactory提供的内容?

URL url = new URL(fileLocation);
URLConnection connection = url.openConnection();
// Prevent JavaWebStart from returning cached copy.
connection.setUseCaches(false);

// Now fetch the content, e.g.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder(); 
Document doc = db.parse(connection.getInputStream());

这是我当前使用多个Apache Batik字段的loadSVGDocument()方法:

public void loadSVGDocument(final String url)
{
    System.out.println("THE SVG URL: " + url);
    String oldURI = null;
    if (svgDocument != null)
    {
       oldURI = svgDocument.getURL();
    }

    final ParsedURL newURI = new ParsedURL(oldURI, url);
    String theUrl = newURI.toString();
    fragmentIdentifier = newURI.getRef();

    loader = new DocumentLoader(userAgent);
    nextDocumentLoader = new SVGDocumentLoader(theUrl, loader);
    nextDocumentLoader.setPriority(Thread.NORM_PRIORITY);

    Iterator it = svgDocumentLoaderListeners.iterator();
    while (it.hasNext())
    {
       nextDocumentLoader
          .addSVGDocumentLoaderListener((SVGDocumentLoaderListener) it.next());
    }

    documentLoader = nextDocumentLoader;
    nextDocumentLoader = null;
    documentLoader.run();
}

1 个答案:

答案 0 :(得分:0)

对于任何有兴趣的人,我发现我可以打电话给Batik's

DocumentLoader.loadDocument(URL url, InputStream is)

将setUseCaches标志设为false。这不仅会加载图像,还会相应地将其从缓存中删除。虽然JWS不是最好的解决方案,JWS可以很好地支持我的HTTP头,但这种解决办法已经足够了。