0

我正在尝试使用以下脚本提供静态 .svgz 文件(压缩的 SVG):

http.ListenAndServe(":9090", http.FileServer(http.Dir("/srv/www/htdocs/")))

我收到以下错误:

This page contains the following errors:

error on line 1 at column 1: Encoding error
Below is a rendering of the page up to the first error.

如果我尝试通过 apache 获取相同的文件,则该文件显示正确。

有没有办法解决这个问题?

4

2 回答 2

1

Gohttp.FileServer不会自动添加Content-Encoding嗅探文件。如果文件是预压缩的,您将需要添加适当的值。

您可以添加Content-Encoding: gzip到标题中,并http.ServeFile在您的处理程序中使用。

于 2016-06-24T15:11:55.823 回答
0

Apache Header(在 Chrome 中测试):

HTTP/1.1 200 OK
Date: Fri, 24 Jun 2016 14:56:03 GMT
Server: Apache
Last-Modified: Fri, 24 Jun 2016 14:43:34 GMT
ETag: "443-5360731fd11b2"
Accept-Ranges: bytes
Content-Length: 1091
Keep-Alive: timeout=15, max=98
Connection: Keep-Alive
Content-Type: image/svg+xml
Content-Encoding: gzip

Go Header(在 Chrome 中测试):

HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 1091
Content-Type: image/svg+xml
Last-Modified: Fri, 24 Jun 2016 14:43:34 GMT
Date: Fri, 24 Jun 2016 14:54:56 GMT

Apache 在标头中发送“Content-Encoding: gzip”。

工作代码(深受https://groups.google.com/forum/#!topic/golang-nuts/Upzqsbu2zbo启发)

https://play.golang.org/p/eWxqHt9QbM

于 2016-06-24T15:42:30.310 回答