最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Loading GZIP JSON file using AJAX - Stack Overflow

programmeradmin0浏览0评论

I have gzipped json file using below algorithm (from: java gzip can't keep original file's extension name)

private static boolean press(String inputFileName, String targetFileName){
         boolean pressResult=true;
         int BUFFER = 1024*4;
         byte[] B_ARRAY = new byte[BUFFER]; 
         FileInputStream fins=null;
         FileOutputStream fout=null;
         GZIPOutputStream zout=null;
         try{
             File srcFile=new File(inputFileName);
             fins=new FileInputStream (srcFile);
             File tatgetFile=new File(targetFileName);
             fout = new FileOutputStream(tatgetFile);
             zout = new GZIPOutputStream(fout);
             int number = 0; 
             while((number = fins.read(B_ARRAY, 0, BUFFER)) != -1){
                 zout.write(B_ARRAY, 0, number);  
             }
         }catch(Exception e){
             e.printStackTrace();
             pressResult=false;
         }finally{
             try {
                zout.close();
                fout.close();
                fins.close();
            } catch (IOException e) {
                e.printStackTrace();
                pressResult=false;
            }
         }
         return pressResult;
    }

I am returning the JSON

response.setHeader("Content-Type", "application/json");
response.setHeader("Content-Encoding", "gzip");
response.setHeader("Vary", "Accept-Encoding");
response.setContentType("application/json");
response.setHeader("Content-Disposition","gzip");
response.sendRedirect(filePathurl);

or

request.getRequestDispatcher(filePathurl).forward(request, response);

Trying to access the JSON object using AJAX code as below:

$.ajax({
    type : 'GET',
    url : url,
    headers : {'Accept-Encoding' : 'gzip'},
    dataType : 'text',

The output I see is the binary data, not the depressed JSON string. Any suggestion on how to make this work? Note that the Browsers I am using (IE, Chrome, FF) supports gzip as all my static contents which are gzipped by Apache are rendered correctly.

I have gzipped json file using below algorithm (from: java gzip can't keep original file's extension name)

private static boolean press(String inputFileName, String targetFileName){
         boolean pressResult=true;
         int BUFFER = 1024*4;
         byte[] B_ARRAY = new byte[BUFFER]; 
         FileInputStream fins=null;
         FileOutputStream fout=null;
         GZIPOutputStream zout=null;
         try{
             File srcFile=new File(inputFileName);
             fins=new FileInputStream (srcFile);
             File tatgetFile=new File(targetFileName);
             fout = new FileOutputStream(tatgetFile);
             zout = new GZIPOutputStream(fout);
             int number = 0; 
             while((number = fins.read(B_ARRAY, 0, BUFFER)) != -1){
                 zout.write(B_ARRAY, 0, number);  
             }
         }catch(Exception e){
             e.printStackTrace();
             pressResult=false;
         }finally{
             try {
                zout.close();
                fout.close();
                fins.close();
            } catch (IOException e) {
                e.printStackTrace();
                pressResult=false;
            }
         }
         return pressResult;
    }

I am returning the JSON

response.setHeader("Content-Type", "application/json");
response.setHeader("Content-Encoding", "gzip");
response.setHeader("Vary", "Accept-Encoding");
response.setContentType("application/json");
response.setHeader("Content-Disposition","gzip");
response.sendRedirect(filePathurl);

or

request.getRequestDispatcher(filePathurl).forward(request, response);

Trying to access the JSON object using AJAX code as below:

$.ajax({
    type : 'GET',
    url : url,
    headers : {'Accept-Encoding' : 'gzip'},
    dataType : 'text',

The output I see is the binary data, not the depressed JSON string. Any suggestion on how to make this work? Note that the Browsers I am using (IE, Chrome, FF) supports gzip as all my static contents which are gzipped by Apache are rendered correctly.

Share Improve this question edited May 23, 2017 at 12:01 CommunityBot 11 silver badge asked Apr 2, 2013 at 15:27 ad-infad-inf 1,5704 gold badges30 silver badges54 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

By using:

response.sendRedirect(filePathurl);

You are creating another request/response. The headers you have defined are no longer associated with the file that actually gets sent.

Rather than sending a redirect, you need to load up your file and stream it in the same response.

Use Fiddler or another request viewer to see this.

We can not override Accept-Encoding from browser/js etc anymore.

发布评论

评论列表(0)

  1. 暂无评论