22
33import com .zhazhapan .config .JsonParser ;
44import com .zhazhapan .modules .constant .ValueConsts ;
5-
5+ import org .htmlcleaner .CleanerProperties ;
6+ import org .htmlcleaner .DomSerializer ;
7+ import org .htmlcleaner .HtmlCleaner ;
8+ import org .htmlcleaner .TagNode ;
9+ import org .jsoup .Jsoup ;
10+ import org .w3c .dom .Document ;
11+
12+ import javax .xml .parsers .ParserConfigurationException ;
13+ import javax .xml .xpath .XPath ;
14+ import javax .xml .xpath .XPathExpressionException ;
15+ import javax .xml .xpath .XPathFactory ;
616import java .io .IOException ;
717import java .io .InputStream ;
818import java .net .*;
9- import java .util .Random ;
1019
1120/**
1221 * @author pantao
@@ -16,6 +25,73 @@ public class NetUtils {
1625
1726 private NetUtils () {}
1827
28+ /**
29+ * 获取ip归属地
30+ *
31+ * @param ip ip地址
32+ *
33+ * @return 归属地
34+ *
35+ * @throws IOException 异常
36+ * @throws XPathExpressionException 异常
37+ * @throws ParserConfigurationException 异常
38+ */
39+ public static String getLocationByIp (String ip ) throws IOException , XPathExpressionException ,
40+ ParserConfigurationException {
41+ return evaluate (ValueConsts .IP_REGION_XPATH , getHtmlFromUrl ("http://ip.chinaz.com/" + ip ));
42+ }
43+
44+ /**
45+ * XPath解析HTML内容
46+ *
47+ * @param xpath xpath表达式
48+ * @param html html内容
49+ *
50+ * @return 解析结果
51+ *
52+ * @throws XPathExpressionException 异常
53+ * @throws ParserConfigurationException 异常
54+ */
55+ public static String evaluate (String xpath , String html ) throws XPathExpressionException ,
56+ ParserConfigurationException {
57+ HtmlCleaner hc = new HtmlCleaner ();
58+ TagNode tn = hc .clean (html );
59+ Document document = new DomSerializer (new CleanerProperties ()).createDOM (tn );
60+ XPath xPath = XPathFactory .newInstance ().newXPath ();
61+ return xPath .evaluate (xpath , document );
62+ }
63+
64+ /**
65+ * 获取网页内容
66+ *
67+ * @param url 链接
68+ *
69+ * @return {@link String}
70+ *
71+ * @throws IOException 异常
72+ */
73+ public static String getHtmlFromUrl (String url ) throws IOException {
74+ return getDocumentFromUrl (url ).html ();
75+ }
76+
77+ /**
78+ * 获取HTML文档
79+ *
80+ * @param url 链接
81+ *
82+ * @return {@link org.jsoup.nodes.Document}
83+ *
84+ * @throws IOException 异常
85+ */
86+ public static org .jsoup .nodes .Document getDocumentFromUrl (String url ) throws IOException {
87+ return Jsoup .connect (url ).get ();
88+ }
89+
90+ /**
91+ * 获取计算机名
92+ *
93+ * @return 计算机名
94+ */
1995 public static String getComputerName () {
2096 return System .getenv ().get ("COMPUTERNAME" );
2197 }
@@ -67,7 +143,7 @@ public static String getMacAddress() throws UnknownHostException, SocketExceptio
67143 }
68144
69145 /**
70- * 获取公网IP
146+ * 获取公网IP和归属地
71147 *
72148 * @return 公网ip、address,如:{"ip":"127.0.0.1","address":"you ip location"}
73149 */
@@ -162,8 +238,7 @@ public static InputStream getInputStreamOfUrl(URL url) throws IOException {
162238 public static InputStream getInputStreamOfConnection (HttpURLConnection connection ) throws IOException {
163239 connection .setConnectTimeout (1000 * 6 );
164240 connection .setRequestProperty ("Charset" , "UTF-8" );
165- connection .setRequestProperty ("User-Agent" , ValueConsts .USER_AGENT [new Random ().nextInt (ValueConsts
166- .USER_AGENT .length )]);
241+ connection .setRequestProperty ("User-Agent" , ValueConsts .USER_AGENT [0 ]);
167242 connection .setRequestProperty ("Connection" , "Keep-Alive" );
168243 connection .setRequestProperty ("Accept" , "*/*" );
169244 return connection .getInputStream ();
0 commit comments