熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

java抓取網頁內容--生成靜態頁面

2022-06-13   來源: Java核心技術 

  

  private static String getStaticPage(String surl) {

  String htmlContent = ;

  try {

  javaioInputStream inputStream;

  URL url = new URL(surl);

  HttpURLConnection connection = (HttpURLConnection) urlopenConnection();

  nnect();

  inputStream = connectiongetInputStream();

  byte bytes[] = new byte[ * ];

  int index = ;

  int count = inputStreamread(bytes index * );

  while (count != ) {

  index += count;

  count = inputStreamread(bytes index );

  }

  htmlContent = new String(bytes UTF);

  connectiondisconnect();

  } catch (Exception ex) {

  exprintStackTrace();

  }

  return htmlContenttrim();

  }

  String src= getStaticPage();

  File file = new File(d:\l);

  FileWriter resultFile = new FileWriter(file);

  PrintWriter myFile = new PrintWriter(resultFile);

  //寫文件

  myFileprintln(src);

  resultFileclose();

  myFileclose();


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26121.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.