java

位置:IT落伍者 >> java >> 浏览文章

java抓取网页内容--生成静态页面


发布日期:2022年02月05日
 
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();

               

上一篇:Java输入输出流详解

下一篇:JAVA中mark()和reset()用法