// 简易JAVA获取网页有效邮箱地址 ---by 77
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class solo7 {
public static void main(String args[]){
new Win();
}
}
class Win extends Frame implements ActionListener,Runnable{
Button button;
URL url;
TextField text;
TextArea area;
byte b[]=new byte[118];
Thread thread;
Win(){
text=new TextField(20);
area=new TextArea(12,12);
button=new Button("确定");
button.addActionListener(this);
thread=new Thread(this);
Panel p=new Panel();
p.add(new Label("输入网址:"));
p.add(text);
p.add(button);
add(area,BorderLayout.CENTER);
add(p,BorderLayout.NORTH);
setBounds(60,60,360,300);
setVisible(true);
validate();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e){
if(!(thread.isAlive()))
thread=new Thread(this);
try{
thread.start();
}
catch(Exception ee){
text.setText("正在读取"+url);
}
}
private boolean isNameAdressFormat(String email){
boolean isExist = false;
Pattern p = Pattern.compile("//w{1,}@//w{1,}
/56//w{1,}");
Matcher m = p.matcher(email);
boolean b = m.matches();
if(b) {
isExist=true;
} else {
isExist=false;
}
return isExist;
}
public void run(){
try{
int n=-1;
area.setText(null);
url=new URL(text.getText().trim());
InputStream in=url.openStream();
String s="";
while((n=in.read(b))!=-1){
s+=new String(b,0,n);
}
StringTokenizer fenxi=new StringTokenizer(s,"
:=,?!。 :<>&()[]{}/"/'");
int i=fenxi.countTokens();
area.append("共有"+i+"个字符串,有效邮箱如
下:");
while(fenxi.hasMoreTokens()){
String str=fenxi.nextToken();
if(isNameAdressFormat(str))
area.append(str+" ");
}
}
catch(MalformedURLException e1){
text.setText(""+e1);
return;
}
catch(IOException e1){
text.setText(""+e1);
return;
}
}
}