【问题标题】:How to get the correct .xls file from varnish cache?如何从清漆缓存中获取正确的 .xls 文件?
【发布时间】:2012-10-11 20:11:17
【问题描述】:

我使用清漆缓存一些文件,如 *.doc *.png *.xls。

当我从缓存中获取文件但 *.xls 时,它运行良好。

我的 URI 类似于 /attachment/show?fileId=ewer232ewe2121eeddsd。当我从缓存中请求一个 .xls 文件时,它会返回一个名为 show whitout 扩展名的文件。

我的服务器代码是:

if (StringUtil.null2Trim(attachment.getExtension()).equals("doc")
                    || StringUtil.null2Trim(attachment.getExtension()).equals("docx")
                    || StringUtil.null2Trim(attachment.getExtension()).equals("xlsx")
                    || StringUtil.null2Trim(attachment.getExtension()).equals("xls")) {
                response.setHeader("Content-Disposition", "attachment; filename=\""
                        + StringUtil.gbk2Iso(attachment.getName()) + "\"");
                if (StringUtil.null2Trim(attachment.getExtension()).indexOf("doc") != -1) {
                    response.setContentType("application/msword");
                }
                if (StringUtil.null2Trim(attachment.getExtension()).indexOf("xls") != -1) {
                    response.setContentType("application/vnd.ms-excel");
                }

            } else {
                if (StringUtil.null2Trim(attachment.getExtension()).equals("jpg")) {
                    response.setContentType("image/jpeg");
                } else if (StringUtil.null2Trim(attachment.getExtension()).equals("png")) {
                    response.setContentType("image/x-png");
                } else {
                    response.setContentType("image/" + attachment.getExtension());
                }
            }

我的问题是:为什么我不能得到像attachment.getName()+".xls"这样的正确文件全名,以及如何解决。

PS: 有没有办法在 varnish(vcl) 中设置 ContentType?

【问题讨论】:

  • 这听起来像是后端服务器小程序的问题,而不是 Varnish。服务器返回的确切标头是什么?
  • @Ketola 是的,服务器设置了错误的标头。现在它运行良好。

标签: java caching varnish


【解决方案1】:

让我回答最后一个问题;您可以覆盖 Varnish 中的任何 HTTP 响应标头。

使用以下 VCL sn-p:

sub vcl_fetch {
    if (req.url ~ ".xls$") {
        set beresp.http.content-type = "application/vnd.ms-excel";
    }
}

通常,您可以通过“set beresp.http.XXX = YYY;”随意添加和删除标题或“取消设置 beresp.http.XXX;”在 vcl_fetch 中。

对于您的主要问题,我会探讨添加 MIME 信封(标题 example here)是否有帮助。

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 2011-12-26
    • 2014-04-07
    • 2016-12-17
    • 2018-08-07
    • 2012-04-03
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    相关资源
    最近更新 更多