简体   繁体   English

Java AJAX服务器,如何压缩JSON响应

[英]Java AJAX server, how to zip JSON responses

I have a large response which is a JSON object at least 88Kb, I am having problems receiving this data on the clients. 我有一个很大的响应,它是一个至少88Kb的JSON对象,我在客户端上接收此数据时遇到问题。

I would like to zip the content in the Java application and send back the zip, I've done this before when using PHP on the server to zip large content and the browser then unzips it. 我想将Java应用程序中的内容压缩并发送回zip,在服务器上使用PHP压缩大型内容然后浏览器解压缩之前,我已经完成了此操作。

At present the JSON string is built up in a StringBuilder object. 目前,JSON字符串是在StringBuilder对象中构建的。 If this idea is flawed what else could I do? 如果这个想法有缺陷,我该怎么办? The JSON object contains status information for a large system and I need frequent updates to be sent. JSON对象包含大型系统的状态信息,我需要频繁发送更新。

Edit...I've progressed the problem, if the size of the JSON is > 512 bytes then I pass the StringBuffer onto the function below: 编辑...我已经解决了这个问题,如果JSON的大小> 512字节,那么我将StringBuffer传递给下面的函数:

    public StringBuilder zipStringBuilder(StringBuilder sbSource) {
        StringBuilder sbZipped = null;

        try {
            byte[] arybytSource = sbSource.toString().getBytes();
            byte[] arybytBuffer = new byte[ZIP_BUFFER];
            Deflater objDeflater = new Deflater();
            objDeflater.setInput(arybytSource);
            ByteArrayOutputStream baos = new ByteArrayOutputStream(arybytSource.length);
            objDeflater.finish();           
            while( !objDeflater.finished() ) {
                int intCount = objDeflater.deflate(arybytBuffer);
                baos.write(arybytBuffer, 0, intCount);
            }
            baos.close();
            sbZipped = new StringBuilder();         
            sbZipped.append(baos.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sbZipped;
    }

The HTTP headers are as follows: HTTP标头如下:

    HTTP/1.0 200
    Date: Fri Nov 13 14:47:06 GMT 2015
    Access-Control-Allow-Origin: *
    Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT
    Access-Control-Allow-Credentials: true
    Keep-Alive: timeout=2, max=100
    Cache-Control: no-cache
    Pragma: no-cache
    Content-Encoding: zip
    Content-type: application/json;charset=utf-8

But I don't receive this or the browser doesn't understand it? 但是我没有收到或者浏览器不理解?

I've also tried: 我也尝试过:

    Content-Encoding: gzip

With same result. 结果相同。

如果您的服务器位于apache Web服务器之后,请打开apache inflate命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM