文件服务经过zuul无法设置Content-Length

在下载文件的接口中设置Content-Length

···
String filePath = "/home/img/123456.jpg"
File file = new File(filePath);
response.addHeader("Content-Length", String.valueOf(file.length()));
···

直接访问文件服务接口,接口的Response Headers可以中看到Content-Length的值

Content-Disposition: attachment;filename=123456.jpg
Content-Length: 125997
Content-Type: application/x-download
Date: Mon, 15 Jul 2019 08:28:01 GMT

但是经过zuul网关之后,接口的Response Headers就没有这个值了

Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Authenticator
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Authenticator
Cache-Control: private
Content-Disposition: attachment;filename=123456.jpg
Content-Type: application/x-download
Date: Mon, 15 Jul 2019 06:55:43 GMT
Expires: Thu, 01 Jan 1970 08:00:00 CST
Transfer-Encoding: chunked
X-Application-Context: demo-zuul:443

可以看到此处多了一个参数Transfer-Encoding: chunked,可以简单的理解一下,如果有Transfer-Encoding: chunked存在,那么Content-Length就不存在,问题就出在这里,经过了zuul的接口会默认设置一个Transfer-Encoding: chunked

解决办法:只需要在zuul的配置文件中加入如下参数即可

zuul.set-content-length = true

再次访问接口,查看Response Headers的参数,有了Content-Length

Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Authenticator
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Authenticator
Cache-Control: private
Content-Disposition: attachment;filename=123456.jpg
Content-Length: 125997
Content-Type: application/x-download
Date: Mon, 15 Jul 2019 07:51:45 GMT
Expires: Thu, 01 Jan 1970 08:00:00 CST
X-Application-Context: demo-zuul:443

发表评论

电子邮件地址不会被公开。 必填项已用*标注