Loading GZIP JSON file using AJAX
Asked Answered
T

2

3

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

private static boolean compress(String inputFileName, String targetFileName){
         boolean compressResult=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();
             compressResult=false;
         }finally{
             try {
                zout.close();
                fout.close();
                fins.close();
            } catch (IOException e) {
                e.printStackTrace();
                compressResult=false;
            }
         }
         return compressResult;
    }

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 decompressed 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.

Titograd answered 2/4, 2013 at 15:27 Comment(0)
Z
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.

Zymogen answered 2/4, 2013 at 15:33 Comment(1)
thanks for your response. Fiddler confirms your view; I tried forward method also, in which fiddler shows the correct header, and when I view the uncompressed text, i can see the JSON; but the AJAX code is not reading the output as JSON. I am looking for solution (if feasible) to avoid reading and streaming the file.Titograd
L
-1

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

Loon answered 20/3 at 23:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.