java 读取网页源码;
输入标准为 http://www.xxx.com
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.DataInputStream; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import org.omg.CORBA.TCKind; public class web extends JFrame implements ActionListener{ /** * @param args */ JTextField t1=new JTextField(30); JTextArea t2=new JTextArea(); JButton b1=new JButton("open"); JPanel p=new JPanel(); public web(){ p.add(t1); p.add(b1); JScrollPane jp=new JScrollPane(t2); b1.addActionListener(this); getContentPane().add(p,"North"); getContentPane().add(jp,"Center"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(500, 400); setVisible(true); setTitle("read url"); } public static void main(String[] args) { // TODO Auto-generated method stub web webH=new web(); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub readURL(t1.getText()); } private void readURL(String URLName) { // TODO Auto-generated method stub try { URL url=new URL(URLName); URLConnection tc=url.openConnection(); tc.connect(); DataInputStream dis=new DataInputStream(tc.getInputStream()); String inputLineString; while ((inputLineString=dis.readLine())!=null) { t2.append(inputLineString+"\n"); } } catch (MalformedURLException e) { // TODO: handle exception e.printStackTrace(); }catch (IOException e1) { e1.printStackTrace(); // TODO: handle exception } } }