zoukankan      html  css  js  c++  java
  • 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
    		}
    	}
    
    }
    


     

  • 相关阅读:
    驯服 Tiger: 并发集合 超越 Map、Collection、List 和 Set
    模块化Java:声明式模块化
    模块化Java:静态模块化
    用 Apache Tika 理解信息内容
    Refactoring: Encapsulate Collection
    新型的几乎万能的数据结构CDO
    CDO数据结构基础(1) 转载
    模块化Java简介(转载infoq)
    模块化Java:动态模块化
    责任链模式(C++)
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3003809.html
Copyright © 2011-2022 走看看