开发者

Cache file mapped by url in the browser

开发者 https://www.devze.com 2023-03-13 15:23 出处:网络
I have a method mapped in a url like /foo/*/image which downloads an image, and I want the browser to cache that picture. But I\'m not getting it. I can see in Firefox with firebug that the request is

I have a method mapped in a url like /foo/*/image which downloads an image, and I want the browser to cache that picture. But I'm not getting it. I can see in Firefox with firebug that the request is not being cached, and it also happens in Chrome.

I'm trying to set Cache-Control to "max-age=3600, public" in the method, but it doesn't seem to do anything.

Below is a piece of code of the method called in the controller made by Spring-MVC.

Can anyone help me?

开发者_JAVA技巧Thanks.

@RequestMapping(value="/foo/{id}/image", method=RequestMethod.GET)
    public void showImage(
            @PathVariable("id") String id,
            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws Exception{

        //add image to the response
        service.read(id, response);

        //mark the response as cacheable
        HttpServletResponse httpResp = ((HttpServletResponse) response);
        httpResp.setHeader("Cache-Control", "max-age=3600, public");
    }


Consider using <mvc:resources>. There you can set up caching.

Apart from that, make sure your request doesn't go through a proxy that overrides headers, and make sure the response isn't redirected.

0

精彩评论

暂无评论...
验证码 换一张
取 消