获取本机IP并发送到指定的Email,本例可直接运行,本例中所用到的Email地址及密码都是可行的
package web.play.rss.util;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class GetLocalIP {
private int webDepth = 2;
private int intThreadNum = 10;
private String strHomePage = "";
private String myDomain;
private String fPath = "web";
private ArrayList<String> arrUrls = new ArrayList<String>();
private ArrayList<String> arrUrl = new ArrayList<String>();
private Hashtable<String, Integer> allUrls = new Hashtable<String, Integer>();
private Hashtable<String, Integer> deepUrls = new Hashtable<String, Integer>();
private int intWebIndex = 0;
private String charset = "GBK";
private String report = "";
private long startTime;
private int webSuccessed = 0;
private int webFailed = 0;
private String searchKey = "";
private List searchedUrl = new ArrayList();
public List searchedKey = new ArrayList();
private boolean searchCurrent = false;
private boolean saveUrl = true;
private boolean saveSearchKey = true;
private boolean debug = false;
private void setDebug(boolean _d) {
this.debug = _d;
}
public void setSaveSearchKey(boolean saveSearchKey) {
this.saveSearchKey = saveSearchKey;
}
public void setSaveUrl(boolean saveUrl) {
this.saveUrl = saveUrl;
}
public void setSearchCurrent(boolean searchCurrent) {
this.searchCurrent = searchCurrent;
}
public String getSearchKey() {
return searchKey;
}
public List getSearchedKey() {
return searchedKey;
}
public List getSearchedUrl() {
return searchedUrl;
}
public GetLocalIP(){
}
public GetLocalIP(String s, String key) {
this.strHomePage = s;
this.searchKey = key;
}
public String getHomePage() {
return this.strHomePage;
}
public GetLocalIP(String s, int i) {
this.strHomePage = s;
this.webDepth = i;
}
public synchronized void addWebSuccessed() {
webSuccessed++;
}
public synchronized void addWebFailed() {
webFailed++;
}
public synchronized void addReport(String s) {
try {
report += s;
PrintWriter pwReport = new PrintWriter(new FileOutputStream(
"report.txt"));
pwReport.println(report);
pwReport.close();
} catch (Exception e) {
System.out.println("aa");
}
}
public synchronized String getAUrl() {
String tmpAUrl = arrUrls.get(0);
arrUrls.remove(0);
return tmpAUrl;
}
public synchronized String getUrl() {
String tmpUrl = arrUrl.get(0);
arrUrl.remove(0);
return tmpUrl;
}
public synchronized Integer getIntWebIndex() {
intWebIndex++;
return intWebIndex;
}
public void getWebByHomePage() {
System.out.println("Translate html start ... ...");
if (this.searchCurrent) {
searchKey(getContent(this.getHomePage()), this.getSearchKey());
}
startTime = System.currentTimeMillis();
this.myDomain = getDomain();
if (myDomain == null) {
System.out.println("Wrong input!");
return;
}
arrUrls.add(strHomePage);
arrUrl.add(strHomePage);
allUrls.put(strHomePage, 0);
deepUrls.put(strHomePage, 1);
String tmp = getAUrl();
this.getWebByUrl(tmp, charset, allUrls.get(tmp) + "");
int i = 0;
for (i = 0; i < intThreadNum; i++) {
new Thread(new Processer(this)).start();
}
while (true) {
if (arrUrls.isEmpty() && Thread.activeCount() == 1) {
long finishTime = System.currentTimeMillis();
long costTime = finishTime - startTime;
System.out.println("Translate html Finished !");
/* System.out.println("Start time = " + startTime + " "
+ "Finish time = " + finishTime + " "
+ "Cost time = " + costTime + "ms");
// addReport("Start time = " + startTime + " "
// + "Finish time = " + finishTime + " "
// + "Cost time = " + costTime + "ms" + "\n");
System.out.println("Total url number = "
+ (webSuccessed + webFailed) + " Successed: "
+ webSuccessed + " Failed: " + webFailed);
*/
String strIndex = "";
String tmpUrl = "";
while (!arrUrl.isEmpty()) {
tmpUrl = getUrl();
strIndex += "Web depth:" + deepUrls.get(tmpUrl)
+ " Filepath: " + fPath + "/web"
+ allUrls.get(tmpUrl) + ".htm" + " url:" + tmpUrl
+ "\n\n";
}
try {
PrintWriter pwIndex = new PrintWriter(new FileOutputStream(
"fileindex.txt"));
pwIndex.println(strIndex);
pwIndex.close();
} catch (Exception e) {
System.out.println("bb");
}
break;
}
}
}
private String getContent(String strUrl) {
try {
URL pageUrl = new URL(strUrl);
BufferedReader reader = new BufferedReader(new InputStreamReader(
pageUrl.openStream()));
String line;
StringBuffer pageBuffer = new StringBuffer();
while ((line = reader.readLine()) != null) {
pageBuffer.append(line);
if (this.debug) {
System.out.println(line);
}
}
return pageBuffer.toString();
} catch (Exception e) {
}
return null;
}
public void getWebByUrl(String strUrl, String charset, String fileIndex) {
try {
URL url = new URL(strUrl);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
InputStream is = null;
is = url.openStream();
BufferedReader bReader = new BufferedReader(new InputStreamReader(
is));
StringBuffer sb = new StringBuffer();
String rLine = null;
String tmp_rLine = null;
while ((rLine = bReader.readLine()) != null) {
tmp_rLine = rLine;
int str_len = tmp_rLine.length();
if (str_len > 0) {
sb.append("\n" + tmp_rLine);
if (deepUrls.get(strUrl) < webDepth)
getUrlByString(tmp_rLine, strUrl);
}
tmp_rLine = null;
}
is.close();
if (!this.searchCurrent) {
searchKey(getContent(strUrl), this.getSearchKey());
}
if (this.saveUrl) {
searchedUrl.add(strUrl);
}
addWebSuccessed();
} catch (Exception e) {
System.out.println("Get web failed! " + strUrl);
addWebFailed();
}
}
public String getDomain() {
String reg = "(?<=http\\://[a-zA-Z0-9]{0,100}[.]{0,1})[^.\\s]*?\\.(com|cn|net|org|biz|info|cc|tv)";
Pattern p = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(strHomePage);
boolean blnp = m.find();
if (blnp == true) {
return m.group(0);
}
return null;
}
public void searchKey(String content, String key) {
try {
Pattern p = Pattern.compile(key, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(content);
boolean blnp = m.find();
while (blnp == true) {
String searched = m.group(0);
if (this.saveSearchKey && searched != null
&& !searched.trim().replace(" ", "").equals("null")
&& !searched.trim().replace(" ", "").equals("")) {
searchedKey.add(searched);
}
blnp = m.find();
}
} catch (Exception e) {
}
}
public void getUrlByString(String inputArgs, String strUrl) {
String tmpStr = inputArgs;
String regUrl = "(?<=(href=)[\"]?[\']?)[http://][^\\s\"\'\\?]*("
+ myDomain + ")[^\\s\"\'>]*";
Pattern p = Pattern.compile(regUrl, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(tmpStr);
boolean blnp = m.find();
// int i = 0;
while (blnp == true) {
if (!allUrls.containsKey(m.group(0))) {
arrUrls.add(m.group(0));
arrUrl.add(m.group(0));
allUrls.put(m.group(0), getIntWebIndex());
deepUrls.put(m.group(0), (deepUrls.get(strUrl) + 1));
}
tmpStr = tmpStr.substring(m.end(), tmpStr.length());
m = p.matcher(tmpStr);
blnp = m.find();
}
}
public String getInternetIP(String getIpServiceUrl ,String getIpServiceUrl2,String key){
String ip = "";
try{
GetLocalIP gw = new GetLocalIP(getIpServiceUrl, key);
gw.setSearchCurrent(true);
gw.getWebByHomePage();
List list = gw.getSearchedKey();
for (int i = 0; i < list.size(); i++) {
ip += list.get(i).toString() + "~@##" ;
}
/*if (list.size() == 0) {
list = gw.getSearchedUrl();
System.out.println(list.size());
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}*/
}catch (Exception e) {
System.out.println("get ip by first url failed ,try the second ... ...");
GetLocalIP gw = new GetLocalIP(getIpServiceUrl2, key);
gw.setSearchCurrent(true);
gw.getWebByHomePage();
List list = gw.getSearchedKey();
for (int i = 0; i < list.size(); i++) {
ip += list.get(i).toString() + "~@##" ;
}
}
return ip;
}
public boolean sendIpToMail(String ip){
System.out.println("Email is sending ... ...");
boolean ok = false;
try {
Properties p = new Properties(); //Properties p = System.getProperties();
p.put("mail.smtp.auth", "true");
p.put("mail.transport.protocol", "smtp");
p.put("mail.smtp.host", "smtp.163.com");
p.put("mail.smtp.port", "25");
//建立会话
Session session = Session.getInstance(p);
Message msg = new MimeMessage(session); //建立信息
msg.setFrom(new InternetAddress("[email protected]")); //发件人
msg.setRecipient(Message.RecipientType.TO,
new InternetAddress("[email protected]")); //收件人
//new InternetAddress("[email protected]")); //收件人
msg.setSentDate(new Date()); // 发送日期
msg.setSubject("Auto:" + ip); // 主题
msg.setText(ip); //内容
// 邮件服务器进行验证
Transport tran = session.getTransport("smtp");
tran.connect("smtp.163.com", "fbote_test", "fbote123456");
// bluebit_cn是用户名,xiaohao是密码
tran.sendMessage(msg, msg.getAllRecipients()); // 发送
System.out.println("Send Email successful !");
ok = true;
} catch (Exception e) {
e.printStackTrace();
}
return ok;
}
public static void main(String[] args) {
// String reg = "((\\d){1,3}\\.(\\d){1,3}\\.(\\d){1,3}\\.(\\d){1,3})|(>\\D{1,4}省\\D{1,5}\\s+\\D{1,4}<)";
// String str = "<h1>ip38.com(ip地址查询)</h1><h2><LI>您的本机IP地址:121.8.15.211 来自:</strong><span id=\"ipad\"> 稍等,查询中.... </span></LI></h2></form> <div id=\"ipadcode\" style=\"display:none\">广东省广州市 电信</div><div align=\"center\"><script language=\"javascript\"> ";
//
// Pattern p = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
// Matcher m = p.matcher(str);
// while(m.find()){
// System.out.println("======"+m.group());
// }
String url = "http://www.ip38.com";
String url2 = "http://www.987654.com/ip.asp";
String key = "((\\d){1,3}\\.(\\d){1,3}\\.(\\d){1,3}\\.(\\d){1,3})|(>\\D{1,4}省\\D{1,5}\\s+\\D{1,4}<)";
//key = "(\\d){1,3}\\.(\\d){1,3}\\.(\\d){1,3}\\.(\\d){1,3}";
GetLocalIP gter = new GetLocalIP();
String ip = gter.getInternetIP(url ,url2, key);
gter.sendIpToMail(ip);
}
class Processer implements Runnable {
GetLocalIP gw;
public Processer(GetLocalIP g) {
this.gw = g;
}
public void run() {
// Thread.sleep(5000);
while (!arrUrls.isEmpty()) {
String tmp = getAUrl();
getWebByUrl(tmp, charset, allUrls.get(tmp) + "");
}
}
}
}