java8 实现网页下载,大致分为4步,分别是:给定目标网页链接->与目标主机建立连接->读入网页文件流->写入本地文件 。会用到java io 和 java net库。程序代码如下,留有注释。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| import java.io.*; import java.net.MalformedURLException; import java.net.URL;
public class test { public static void main(String[] args) { try { new test().downloadPage(); } catch (IOException e) { e.printStackTrace(); } }
private String downloadPage() throws IOException { String url = "https://hexo.io/zh-cn/"; String inputLine = null; try { URL pageUrl = new URL(url); BufferedReader br = new BufferedReader( new InputStreamReader(pageUrl.openStream(), "utf-8")); File file = new File("D:\\Test\\maliang\\index.html"); FileOutputStream out = new FileOutputStream(file); OutputStreamWriter write = new OutputStreamWriter(out, "utf-8"); while ((inputLine = br.readLine()) != null) { write.write(inputLine); System.out.println(inputLine); } br.close(); write.close(); System.err.println("下载完毕!"); } catch (MalformedURLException e) { e.printStackTrace(); } return url; } }
|