【发布时间】:2019-02-12 16:34:29
【问题描述】:
我们尝试在服务器端使用 Deflater 输出流和 GZIP 输出流来压缩响应,并在客户端使用 Pako 模块来解压它。使用 deflater 会在浏览器中出现控制台错误“未捕获的异常:无效距离太远”,使用 gzip 会出现“未捕获的异常:无效块类型”。
根据我的阅读,浏览器应该能够解压缩响应本身,但使用“response.addHeader("Content-Encoding", "gzip");”不起作用。没有错误,当浏览器接收到响应时,响应只是没有该标头。
放气器代码:
private static String compress(String str)
{
try
{
final ByteArrayOutputStream out = new ByteArrayOutputStream();
try
{
final DeflaterOutputStream def = new DeflaterOutputStream(out, new Deflater(Deflater.BEST_COMPRESSION, true));
try
{
def.write(str.getBytes());
return new String(Base64.encodeBase64(out.toByteArray()));
}
finally
{def.close();}
}
finally
{out.close();}
}
catch(IOException e)
{
LOGGER.warn("Failed to compress.");
}
return str;
Gzip 代码。
try
{
final ByteArrayOutputStream out = new ByteArrayOutputStream();
try
{
final GZIPOutputStream gzip = new GZIPOutputStream(out);
try
{gzip.write(str.getBytes());}
finally
{gzip.close();}
return out.toString("UTF-8");
}
finally
{out.close();}
}
catch(IOException e)
{LOGGER.warn("Failed to compress.");}
return str;
}
pako 电话
LandAdditions.prototype.loadfields = function() {
let me = this;
me.loadingBanner().show();
var url =
"/rest/accounts/" + app.accountIdBox().val() + "/land_additions/fields";
$.get(url)
.done(function(data) {
me.onLoadFields(JSON.parse(pako.inflateRaw(data, { to: 'string'})));
【问题讨论】:
标签: node.js spring-boot compression deflate