【问题标题】:"Content Encoding Error" when I try to gzip the output当我尝试 gzip 输出时出现“内容编码错误”
【发布时间】:2014-03-03 10:36:26
【问题描述】:

我正在尝试压缩控制器操作的输出以节省一些带宽:

new ByteArrayOutputStream().withStream{ baos ->
  new GZIPOutputStream( baos ).withWriter{ it << m.text.bytes }
  //def gzip = baos.toByteArray().encodeBase64()
  def gzip = new String( baos.toByteArray() )
  response.setHeader 'Content-Type', 'application/x-javascript'
  response.setHeader 'Content-Encoding', 'x-gzip'
  response.outputStream.withStream{ it << gzip }
}

}

当我在浏览器中打开网址时,它会显示给我

未知错误:net::ERR_CONTENT_DECODEDING_FAILED

在 IE 中 或

内容编码错误

在FF中

我错过了什么?

【问题讨论】:

  • 您能检查一下您是否没有使用 utf-8 BOM 保存文件吗? (stackoverflow.com/questions/2223882/…) 你需要一个好的编辑器才能注意到其中的差异。
  • 另外,如果打开了 php error_reporting,这可能会出现问题。
  • 不,不存在BOM问题,与php无关

标签: grails gzip


【解决方案1】:
    def index() {
            response.setHeader 'Content-Type', 'application/x-javascript'
            response.setHeader 'Content-Encoding', 'x-gzip'
            new GZIPOutputStream(response.outputStream).withWriter{ it << "Content comes here" }
    }

还可以考虑在您的 web 应用程序前面使用 web 服务器的功能(例如 apache 的 gzip 模块可以更好地处理这种方式)。您还必须首先检查客户端的功能(客户端请求中的Accept-Encoding 标头)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    • 2021-12-21
    • 2015-11-04
    • 2023-04-08
    • 1970-01-01
    • 2022-06-14
    相关资源
    最近更新 更多