Java – gzip string used for ColdFusion output causes “content encoding error” in the browser

I tried to output gzip content to the browser in a variable First, I'm making this very simple instead of worrying about browsers that don't support gzip In addition, I put it together by studying several methods I can find on the Internet Some of them may be reading the question

<cfsavecontent variable="toGZIP"><html><head><title>Test</title></head><body><h1>Fear my test</h1></body></html></cfsavecontent>

<cfscript>
ioOutput = CreateObject("java","java.io.ByteArrayOutputStream");
gzOutput = CreateObject("java","java.util.zip.GZIPOutputStream");

ioOutput.init();
gzOutput.init(ioOutput);

gzOutput.write(toGZIP.getBytes("UTF-8"),Len(toGZIP.getBytes()));

gzOutput.finish();
gzOutput.close();
ioOutput.flush();
ioOutput.close();

toOutput=ioOutput.toString("UTF-8");
</cfscript>

<cfcontent reset="yes" /><cfheader name="Content-Encoding" value="gzip"><cfheader name="Content-Length" value="#ArrayLen( toOuptut.getBytes() )#" ><cfoutput>#toOuptut#</cfoutput><cfabort />

But I made mistakes in Firefox (as well as chrome and safari)

Does anyone have any ideas?

Operating system: MAC ox-x snow leopard CF: 9-dev network server: Apache

solution

<cfsavecontent variable="toGZIP"><html><head><title>Test</title></head><body><h1>Fear my test</h1></body></html></cfsavecontent>

<cfscript>
ioOutput = CreateObject("java","java.util.zip.GZIPOutputStream");

ioOutput.init();
gzOutput.init(ioOutput);

gzOutput.write(toGZIP.getBytes(),Len(toGZIP.getBytes()));

gzOutput.finish();
gzOutput.close();
ioOutput.flush();
ioOutput.close();

toOutput=ioOutput.toByteArray();
</cfscript>

<cfheader name="Content-Encoding" value="gzip"><cfheader name="Content-Length" value="#ArrayLen(toOutput)#" ><cfcontent reset="yes" variable="#toOutput#" /><cfabort />

Solution

The following line looks completely wrong:

toOutput=ioOutput.toString("UTF-8");

You use utf8 to encode gzip streams The result is junk data If ColdFusion has the option, it is best to set gzip data to binary If you can only set strings, you need not change the encoding of any bytes For example, iso1

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>