GZIPInputstream解決亂碼問題

public static String getHtmlContent(String htmlurl, String charset) {

   StringBuffer sb = new StringBuffer();

   String acceptEncoding = "";

   /* 1.生成 HttpClinet 對象並設置參數 */

   HttpClient httpClient = new HttpClient();

   // 設置 Http 連接超時 5s

   httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(

           5000);

   GetMethod method = new GetMethod(htmlurl);

   // 設置 get 請求超時 5s

   method.getParams()

           .getDoubleParameter(HttpMethodParams.SO_TIMEOUT, 10000);

   // 設置請求重試處理

   method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,

           new DefaultHttpMethodRetryHandler());

   int statusCode;

   try {

       statusCode = httpClient.executeMethod(method);

       // 判斷訪問的狀態碼

       if (statusCode != HttpStatus.SC_OK) {

           return sb.toString();

       } else {

           if (method.getResponseHeader("Content-Encoding") != null)

               acceptEncoding = method

                       .getResponseHeader("Content-Encoding").getValue();

           if (acceptEncoding.toLowerCase().indexOf("gzip") > -1) {

               // 建立gzip解壓工作流

               InputStream is;

               is = method.getResponseBodyAsStream();

              GZIPInputStream gzin = new GZIPInputStream(is);

               InputStreamReader isr = new InputStreamReader(gzin, charset); // 設置讀取流的編碼格式,自定義編碼

               java.io.BufferedReader br = new java.io.BufferedReader(isr);

               String tempbf;

               while ((tempbf = br.readLine()) != null) {

                   sb.append(tempbf);

                   sb.append("\r\n");

               }

               isr.close();

               gzin.close();

               System.out.println(sb);

           } else {

               InputStreamReader isr;

               isr = new InputStreamReader(

                       method.getResponseBodyAsStream(), charset);

               java.io.BufferedReader br = new java.io.BufferedReader(isr);

               String tempbf;

               while ((tempbf = br.readLine()) != null) {

                   sb.append(tempbf);

                   sb.append("\r\n");

               }

               isr.close();

           }

       }

   } catch (HttpException e1) {

       e1.printStackTrace();

   } catch (IOException e1) {

       e1.printStackTrace();

   }

   method.abort();

   method.releaseConnection();

   return sb.toString();

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章