java gzip压缩 http_http请求内容gzip压缩

网页内容可以通过tomcat配置进行gzip,通过java代码也可以进行压缩。

http://code.google.com/p/dysys/source/browse/trunk/CommTestMethod/src/com/method/file/web/GzipHtml.java

/**      * HTML请求      * 请求头信息添加支持gzip压缩      * @param url      * @return      * @throws HttpException      * @throws IOException      */     public static String unGzipHtml(String url) throws HttpException, IOException{         HttpClient http = new HttpClient() ;         String html = "" ;         GetMethod get = new GetMethod(url);         get.addRequestHeader("accept-encoding", "gzip,deflate") ;         try{             int er = http.executeMethod(get);             if(er==200){                 html = getResponseBodyAsString(get) ;             }         } finally {             get.releaseConnection();         }         return html ;     }     /**      * 读取请求内容      * 如果返回内容是压缩内容,解压缩      * @param get      * @return      * @throws IOException      */     private static String getResponseBodyAsString(GetMethod get) throws IOException {         if (get.getResponseBody() != null) {             if(get.getResponseHeader("Content-Encoding") != null                     && get.getResponseHeader("Content-Encoding").getValue().toLowerCase().indexOf("gzip") > -1) {                 //For GZip response                 InputStream is = get.getResponseBodyAsStream();                 GZIPInputStream gzin = new GZIPInputStream(is);                                 InputStreamReader isr = new InputStreamReader(gzin, get.getResponseCharSet());                 java.io.BufferedReader br = new java.io.BufferedReader(isr);                 StringBuffer sb = new StringBuffer();                 String tempbf;                 while ((tempbf = br.readLine()) != null) {                     sb.append(tempbf);                     sb.append("/r/n");                 }                 isr.close();                 gzin.close();                 return sb.toString();             } else {                 return get.getResponseBodyAsString();             }         } else {             return null;         }             }         /**      * html输出      * 如果请求支持gzip      * 输出内容gzip压缩      * 输出头信息写入gzip头信息      * @param response      * @param request      * @param content      * @throws IOException      */     public static void gzipHtml(HttpServletResponse response,HttpServletRequest request, String content)     throws IOException{         byte[] data = content.getBytes("utf-8") ;                 String encoding = request.getHeader("Accept-Encoding");         if(encoding !=null && encoding.indexOf("gzip")>-1){             response.setHeader("Content-Encoding", "gzip") ;             ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(10240) ;             GZIPOutputStream output = null ;             try {                 output = new GZIPOutputStream(byteOutput);                 output.write(data);             } catch (IOException e) {                 throw new RuntimeException("G-Zip failed.", e);             } finally {                 if (output != null) {                     try {                         output.close();                     } catch (IOException e) {}                 }             }             data = byteOutput.toByteArray();         }         response.setContentLength(data.length);         ServletOutputStream os = response.getOutputStream();         os.write(data);         os.close();     }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值