zoukankan      html  css  js  c++  java
  • JAVA获取网页源码

    package ex30;
    
    
    
    import java.awt.*;
    
    import java.awt.event.*;
    
    import java.io.*;
    
    import java.net.*;
    
    import javax.swing.*;
    
    
    
    public class ViewRemoteFile extends JApplet{
    
    	// Button to view the file
    
    	private JButton jbtView = new JButton("View");
    
    	
    
    	// Text field to receive file name
    
    	private JTextField jtfURL = new JTextField(12);
    
    	
    
    	// Text area to store file
    
    	private JTextArea jtaFile = new JTextArea();
    
    	
    
    	// Label to display status
    
    	private JLabel jlblStatus = new JLabel();
    
    	
    
    	/** Initialize the applet */
    
    	public void init(){
    
    		// Create a panel to hold a label, a text field, and a button
    
    		JPanel p1 = new JPanel();
    
    		p1.setLayout(new BorderLayout());
    
    		p1.add(new JLabel("Filename"), BorderLayout.WEST);
    
    		p1.add(jtfURL, BorderLayout.CENTER);
    
    		p1.add(jbtView, BorderLayout.EAST);
    
    		
    
    		// Place text area and panel p to the applet
    
    		setLayout(new BorderLayout());
    
    		add(new JScrollPane(jtaFile), BorderLayout.CENTER);
    
    		add(p1, BorderLayout.NORTH);
    
    		add(jlblStatus, BorderLayout.SOUTH);
    
    		
    
    		// Register listener to handle the "View" button
    
    		jbtView.addActionListener(new ActionListener(){
    
    			public void actionPerformed(ActionEvent e){
    
    				showFile();
    
    			}
    
    		});
    
    	}
    
    	
    
    	private void showFile(){
    
    		java.util.Scanner input = null;  // Use Scanner for text input
    
    		URL url = null;
    
    		
    
    		try{
    
    			// Obtain URL from the text field
    
    			url = new URL(jtfURL.getText().trim());
    
    			
    
    			// Create a Scanner for input stream
    
    			input = new java.util.Scanner(url.openStream());
    
    			
    
    			// Read a line and append the line to the text area
    
    			while(input.hasNext()){
    
    				jtaFile.append(input.nextLine() + "
    ");
    
    			}
    
    			
    
    			jlblStatus.setText("File loaded successfully");
    
    		}
    
    		catch(MalformedURLException ex){
    
    			jlblStatus.setText("URL " + url + " not found");
    
    		}
    
    		catch(IOException e){
    
    			jlblStatus.setText(e.getMessage());
    
    		}
    
    		finally{
    
    			if(input != null)  input.close();
    
    		}
    
    	}
    
    	
    
    }
    

      转自http://bbs.csdn.net/topics/390040684

  • 相关阅读:
    CSS 基础语法
    标签
    HDU 5487 Difference of Languages BFS
    HDU 5473 There was a kingdom 凸包 DP
    HDU 5468 Puzzled Elena 莫比乌斯反演
    BNU 3692 I18n 模拟
    补题列表
    POJ 3241 曼哈顿距离最小生成树 Object Clustering
    UVa 1309 DLX Sudoku
    CodeForces Round #320 Div2
  • 原文地址:https://www.cnblogs.com/kyxyes/p/3970910.html
Copyright © 2011-2022 走看看