获取网页的源码:
package com.atguigu.crud.controller;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class ssssss {
public static void main(String[] a){
try {
URL url=new URL("http://www.baidu.com");
BufferedReader reader=new BufferedReader(new InputStreamReader(url.openStream()));
BufferedWriter writer=new BufferedWriter(new FileWriter("index.html"));
String line;
while((line=reader.readLine())!=null){
System.out.println(line);
writer.write(line);
writer.newLine();
}
reader.close();
writer.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
还有一个更简单的: 需要用到 jsoup-1.7.2.jar
String html = Jsoup.connect("你的网址").get().html();