Java get Web page source code

Source: Internet
Author: User

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 fileprivate JButton Jbtview = new JButton ("View");//Tex T field to receive file nameprivate JTextField jtfurl = new JTextField;//Text area to store fileprivate JTextArea JTA File = new JTextArea ();//Label to display statusprivate JLabel jlblstatus = new JLabel ();/** Initialize the applet */publ IC void init () {//Create a panel to hold a label, a text field, and a buttonjpanel p1 = new JPanel ();p 1.setLayout (New Bord Erlayout ());p 1.add (New JLabel ("Filename"), borderlayout.west);p 1.add (Jtfurl, Borderlayout.center);p 1.add (Jbtview, Borderlayout.east);//Place text area and panel p to the Appletsetlayout (new BorderLayout ()); Add (new JScrollPane (Jtafile) , Borderlayout.center), add (P1, Borderlayout.north), add (Jlblstatus, Borderlayout.south);//Register listener to handle The "View" Buttonjbtview.addactionlistener (New ACtionlistener () {public void actionperformed (ActionEvent e) {showfile ();}});}  private void Showfile () {Java.util.Scanner input = null;  Use Scanner for text inputurl url = null;try{//Obtain URL from the text fieldurl = new URL (jtfurl.gettext (). Trim ());// Create a Scanner for input streaminput = new Java.util.Scanner (Url.openstream ());//Read a line and append the line to th E Text Areawhile (Input.hasnext ()) {Jtafile.append (Input.nextline () + "\ n");} 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 ();}}}

Transfer from http://bbs.csdn.net/topics/390040684

Java get Web page source code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.