使用了多线程
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
class GetWebPageThread extends Thread{
private URL oUrl;
private JTextArea oTextarea;
public GetWebPageThread(String url,JTextArea t){
try{
oUrl = new URL(url);
oTextarea = t;
}
catch(Exception e){
}
} public void run(){
try{
HttpURLConnection con = (HttpURLConnection)oUrl.openConnection();
con.setRequestProperty("user-agent","WokeyiBot+(http://www.wokeyi.net/bot.htm)");
BufferedReader bfReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
String responseText = "";
while((line = bfReader.readLine())!=null){
responseText += line+"\r\n";
}
oTextarea.append(responseText);
}
catch(UnknownHostException e){
JOptionPane.showMessageDialog(null,"找不到主机");
}
catch(Exception e){
e.printStackTrace();
}
}
}public class WebThief extends JPanel implements ActionListener{
JTextField oUrl;
JTextArea oOutput;
JButton oStart;
public static void CreateAndShowGUI(){
JFrame frm = new JFrame();
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
WebThief wt = new WebThief();
wt.AddWidgets();
frm.setContentPane(wt);
frm.pack();
frm.setVisible(true);
} public void actionPerformed(ActionEvent e){
GetWebPageThread[] thread = new GetWebPageThread[100];
for(int i=0;i<thread.length;i++){
thread[i] = new GetWebPageThread(oUrl.getText(),oOutput);
thread[i].run();
}
} private void AddWidgets(){
oUrl = new JTextField("http://",30);
oOutput = new JTextArea(5,30);
oStart = new JButton("开始");
oStart.addActionListener(this);
add(oUrl);
add(new JScrollPane(oOutput));
add(oStart);
} public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
CreateAndShowGUI();
}
});
}
}