首页 > 代码库 > Java的URL来下载网页源码

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来下载网页源码