# 离线数据包
服务端部署离线数据包 (opens new window)介绍了Nginx服务器的配置方式,本篇文章介绍其他几类服务器配置。
# Apache
1)Apache未启用gzip特性时,httpd.conf文件末尾添加
Header set Content-Encoding "gzip"
"expr=%{REQUEST_URI} =~ m#.*.gz$#"
2)Apache启用gzip特性时,需要对gz请求禁用gzip功能,httpd.conf文件在对应的gzip配置处添加
DeflateCompressionLevel 6
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
Header set Content-Encoding "gzip"
# Tomcat
Tomcat不支持在配置文件中设置,因此只能通过代码在拦截器中设置头信息。具体步骤:
- 编写Filter类,对gz请求响应添加“Content-Encoding”头属性
package com.bimface.offline.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
public class GzFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) servletResponse;
//告诉响应头以gzip格式解码,启用浏览器gzip解压
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Encoding", "gzip");
response.setContentType("application/json;charset=utf-8");
try {
filterChain.doFilter(servletRequest, servletResponse);
} catch (ServletException e) {
e.printStackTrace();
}
}
@Override
public void destroy() {
}
}
- Filter打包Jar放入Tomcat\lib目录下
- 对Tomcat的web.xml文件添加全局Filter配置
<filter>
<filter-name>gzFilter</filter-name>
<filter-class>com.bimface.offline.filter.GzFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>gzFilter</filter-name>
<url-pattern>*.gz</url-pattern>
</filter-mapping>
# NodeJS
在NodeJS服务中对gz请求响应添加“Content-Encoding”头属性,类似
response.writeHeader(200,{
'Content-Encoding' : 'gzip',
'content-type' : 'application/octet-stream'
});
例如Server.js:
var port = 8888;
var http = require('http');
var fs = require('fs');
var path = require('path');
const mime = require('mime');
var documentRoot = './databag'; //需要访问的离线数据包文件的存放目录
var server= http.createServer(function(req,res){
var url = req.url;
var file = documentRoot + url;
var extname = path.extname(file);
fs.readFile(file, function(err,data){
if(err){
res.writeHeader(404,{
'content-type' : 'text/html;charset="utf-8"'
});
res.write('<h1>404错误</h1><p>你要找的页面不存在</p>');
res.end();
}else{
if(url.substr(url.length-3, 3) == ".gz"){
res.writeHeader(200,{
'Content-Encoding' : 'gzip',
'Content-type' : 'application/octet-stream'
});
}else{
var contentType = mime.getType(extname);
res.writeHeader(200,{
'Content-type' : contentType
});
}
res.write(data);
res.end();
}
});
}).listen(port);
构件空间关系计算 →