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
    		}
    	}
    
    }
    


     

  • 相关阅读:
    Python 内存泄露 内存回收机制
    decimal 格式化
    iis 6 配置PHP
    按照 in (....) 里面的顺序进行排序
    设计模式之 访问者模式
    与数据库的列信息有关
    win32 IFolderView2::GetCurrentFolderFlags的使用
    MySQL防止重复插入相同记录 insert if not exists
    c++扩展Python(未验证)
    c++ 获取桌面图标的坐标与名称
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3003809.html
Copyright © 2011-2022 走看看