zoukankan      html  css  js  c++  java
  • Tomcat6设置gzip压缩 Java解压缩gzip[z]

    Tomcat6设置gzip压缩 Java解压缩gzip

     

              Tomcat的配置文件conf/server.xml添加如下的后四个属性即可设置将资源进行gzip压缩,有效提高响应速度:

     <Connector port="7087" protocol="HTTP/1.1"

                   connectionTimeout="20000"

                   redirectPort="8443"                 

                   compression="on"

                   compressionMinSize="2048"

                   noCompressionUserAgents="gozilla,traviata"              compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,image/gif,image/jpeg,image/png" />

    利用http://gzip.zzbaike.com/ 测试,出现404 Not found错误好吧,自己测试搜的别人的代码:

    import org.apache.commons.httpclient.HttpClient;

    import org.apache.commons.httpclient.methods.GetMethod;

     

    public class HttpTester {

     

       public static void main(String[] args) throws Exception{

            HttpClient http = new HttpClient();

            GetMethod get = new GetMethod("http://www.baidu.com/");

               try{

                    get.addRequestHeader("accept-encoding", "gzip,deflate");

                    get.addRequestHeader("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Alexa Toolbar; Maxthon 2.0)");

                   int er = http.executeMethod(get);

                   if(er==200){

                        System.out.println(get.getResponseContentLength());

                        String html = get.getResponseBodyAsString();

                        System.out.println(html);

                        System.out.println(html.getBytes().length);

                   }

              }finally{

                      get.releaseConnection();

              }

          }

      }

    要想编译运行这段代码,必须引入3个包:commons-httpclient-3.0.1.jar    commons-logging-1.1.1.jar     和   commons-codec-1.4.jar

    并且String html = get.getResponseBodyAsString(); 最好替换为 InputStream is = get.getResponseBodyAsStream();然后再处理这个输入流(可以利用ByteArrayBuffer,但是需要引入httpcore-4.1.jar也可以不用,反正各种方法)

    另一种测试方法,利用基本的J2SE,不用引入任何第三方包,也是搜的别人的:

    public class Test2 {

     public static void main(String[] args) {

           try {

                URL url = new URL("http://www.baidu.com/");

                HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                conn.setRequestProperty("Accept-Encoding", "gzip,deflate");//如果这里不设置,返回的就不是gzip的数据了,也就不用解压缩了

                conn.connect();

                InputStream in = conn.getInputStream();

                BufferedReader bin = new BufferedReader(new InputStreamReader(in,"GB2312"));

      

                //GZIPInputStream gzin = new GZIPInputStream(in);

             // BufferedReader bin = new BufferedReader(new InputStreamReader(gzin, "GB2312"));  

              String s = null;

              while((s=bin.readLine())!=null){

              System.out.println(s);

              }

                bin.close();

         } catch (Exception e) {

               e.printStackTrace();

        }

      }

    }

    解压缩就是上面注释掉的两行代码

  • 相关阅读:
    MySql模糊查询like通配符使用详细介绍
    使用powershell批量添加Qt的文件(生成pro)
    宏定义的教训
    使用powershell批量添加Keil和IAR的头文件路径
    python和数据科学(Anaconda)
    CDCE913产生任意频率
    QT中检索设定目录下所有指定文件的方法
    QT中将ASCII转换为对应数值的方法
    STM8如何使用自带的bootloader
    QT中使用函数指针
  • 原文地址:https://www.cnblogs.com/jjj250/p/2628732.html
Copyright © 2011-2022 走看看