通過網路獲取頁面返回的資料流
阿新 • • 發佈:2019-01-30
從網路中獲取網頁資料時,網頁有可能使用GZIP壓縮技術對頁面進行壓縮,這樣就會減小通過網路傳輸的資料量,提高瀏覽的速度。因此在獲取網路資料時要對其進行判斷,對GZIP格式的資料使用GZIPInputStream對其特殊處理,否則在獲取資料可能出現亂碼.
import java.io.ByteArrayOutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.zip.GZIPInputStream; import com.lowagie.text.pdf.codec.Base64.InputStream; /** * 從網路中獲取網頁資料 * @author 杜文俊 */ public class stretest { @SuppressWarnings("static-access") public static void main(String[] args) throws Exception { String result = ""; URL url = new URL("http://www.yancao18.com/server/getnode.php?user=test&pwd=test"); //URL url = new URL("http://www.ku6.com/"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(6* 1000);//設定連線超時xml 實體物件 if (conn.getResponseCode() != 200) throw new RuntimeException("請求url失敗"); InputStream inputStream = (InputStream) conn.getInputStream();//得到網路返回的輸入流 if("gzip".equals(conn.getContentEncoding())){ result = readDataForZgip(inputStream, "GBK"); }else { result = readData(inputStream, "GBK"); } conn.disconnect(); System.out.println(result); System.err.println("ContentEncoding: " + conn.getContentEncoding()); } //第一個引數為輸入流,第二個引數為字符集編碼 public static String readData(InputStream inSream, String charsetName) throws Exception{ ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while( (len = inSream.read(buffer)) != -1 ){ outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray(); outStream.close(); inSream.close(); return new String(data, charsetName); } //第一個引數為輸入流,第二個引數為字符集編碼 public static String readDataForZgip(InputStream inStream, String charsetName) throws Exception{ GZIPInputStream gzipStream = new GZIPInputStream(inStream); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer =new byte[1024]; int len = -1; while ((len = gzipStream.read(buffer))!=-1) { outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray(); outStream.close(); gzipStream.close(); inStream.close(); return new String(data, charsetName); } }