Java的URL来下载网页源码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java的URL来下载网页源码相关的知识,希望对你有一定的参考价值。
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
@SuppressWarnings("unused")
public class Test {
//用URL 下载网页的源码
public static void main(String[] args) throws IOException{
URL url = new URL("http://www.cnblogs.com/goodboy-heyang/p/5255818.html?from=timeline&isappinstalled=0");
URLConnection con = url.openConnection();
InputStream in = con.getInputStream();
BufferedInputStream bin = new BufferedInputStream(in);
byte[] buffer = new byte[1024];
int len = 0;
StringBuilder builder = new StringBuilder();
while(-1 != (len = bin.read(buffer))){
builder.append(new String(buffer,0,len));
}
System.out.println(builder);
}
}
以上是关于Java的URL来下载网页源码的主要内容,如果未能解决你的问题,请参考以下文章
Java问题: 怎么样将网页上的数据导入到excel中并下载下来,求,详细源码参考,谢谢