1 package com.peidon.html;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.InputStreamReader;
9 import java.net.HttpURLConnection;
10 import java.net.URL;
11 import java.net.URLConnection;
12
13 import org.jsoup.Jsoup;
14 import org.jsoup.nodes.Document;
15 import org.jsoup.nodes.Element;
16 import org.jsoup.select.Elements;
17
18
19 /**
20 * @author sunshine
21 * @version 1.0
22 * @date:2015年8月15日 上午9:01:13
23 * @description: Java开发搜索引擎爬虫
24 * jsoup 类似jQuery的强大功能,什么方便解析操作HTML DOM 树
25 * 关联jar包 jsoup-1.8.3.jar
26 */
27 public class HttpSoup {
28
29 public static void main(String[] args) {
30
31 //根据网址和页面的编码集 获取网页的源代码
32 String htmlResource = getHtmlResourceByUrl("http://www.ui.cn/", "UTF-8");
33 //System.out.println(htmlResource);
34
35 //解析源代码
36 Document document = Jsoup.parse(htmlResource);
37
38 //获取网页的图片
39 //网页图片标签<img src="" alt="" width="" height="" />
40 Elements elements = document.getElementsByTag("img");
41
42 for(Element element : elements){
43 String imgSrc = element.attr("src");
44 //System.out.println(imgSrc);
45 downImages(imgSrc,"D:\\test\\images\\");
46 System.out.println("下载成功:"+imgSrc);
47 //System.out.println(imgSrc.substring(imgSrc.lastIndexOf("/")));
48 }
49 }
50
51 /**
52 * 根据一个图片的URL地址,通过这个URL批量下载图片到服务器的磁盘
53 * @param imageUrl 要下载的服务器地址
54 * @param filePath 下载完成后保存到服务器的图片地址
55 *
56 */
57 public static void downImages(String imageUrl, String filePath){
58 String fileName = imageUrl.substring(imageUrl.lastIndexOf("/"));
59
60 try {
61 //创建文件的目录
62 File files = new File(filePath);
63 //判断文件是否存在
64 if(!files.exists()){
65 files.mkdirs();
66 }
67 //获取图片文件的下载地址
68 URL url = new URL(imageUrl);
69 //连接网络图片地址
70 HttpURLConnection uc =(HttpURLConnection) url.openConnection();
71 //获取连接的输出流
72 InputStream is = uc.getInputStream();
73
74 //创建文件
75 File file = new File(filePath + fileName);
76 //创建输出流,写入文件
77 FileOutputStream out = new FileOutputStream(file);
78 int i = 0;
79 while((i = is.read()) != -1){
80 out.write(i);
81 }
82 is.close();
83 out.close();
84 } catch (Exception e) {
85 e.printStackTrace();
86 }
87 }
88
89 /**
90 * 根据网址和页面的编码集 获取网页的源代码
91 * @param url
92 * @param encoding
93 * @return
94 */
95 public static String getHtmlResourceByUrl(String url, String encoding){
96
97 //声明一个存储网页源代码的容器
98 StringBuffer buff = new StringBuffer();
99
100 URL urlObj = null;
101 URLConnection uc = null;
102 InputStreamReader in = null;
103 BufferedReader reader = null;
104 try {
105 //建立网络链接
106 urlObj = new URL(url);
107
108 //打开网络链连接
109 uc = urlObj.openConnection();
110
111 //建立网络的输入流
112 in = new InputStreamReader(uc.getInputStream(),encoding);
113
114 //缓冲写入的文件流
115 reader = new BufferedReader(in);
116
117 String tempLine = null;
118
119 //循环读取文件流
120 while((tempLine = reader.readLine()) != null){
121 buff.append(tempLine + "\n"); //循环追加数据
122 }
123
124
125
126 } catch (Exception e) {
127
128 e.printStackTrace();
129 System.out.println("Conection timeout ...");
130 } finally {
131 if(in != null){
132 try {
133 in.close();
134 } catch (IOException e) {
135 e.printStackTrace();
136 }
137 }
138
139 }
140
141 return buff.toString();
142 }
143 }