1 The simplest way to download Web pages
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.net.URL;
public class Exec {
public static void Main (String args[]) {
FileOutputStream Fos;
URL url;
InputStream is;
int i;
try {
FOS = new FileOutputStream ("storedpage.html");
url = new URL ("http://www.baidu.com");
System.out.println (Url.getfile ());
is = Url.openstream ();
i = Is.read ();
while (i > 0) {
Fos.write (i);
i = Is.read ();
}
Fos.close ();
Is.close ();
} catch (IOException e) {
}
}
}
2 Getting search engine results
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.net.URL;
Public class Exec {
public static void Main (String args[]) {
FileOutputStream Fos;
URL url;
InputStream is;
int i;
try {
FOS = new FileOutputStream ("storedpage.html");
url = new URL ("Http://www.baidu.com/s?wd=mp3");
System.out.println (Url.getfile ());
is = Url.openstream ();
i = Is.read ();
while (i > 0) {
Fos.write (i);
i = Is.read ();
}
Fos.close ();
Is.close ();
} catch (IOException e) {
}
}
}
3 Search engine result acquisition program based on swing form
Import Java.awt.BorderLayout;
Import java.awt.Dimension;
Import Java.awt.FlowLayout;
Import Java.awt.Toolkit;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;
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 javax.swing.ScrollPaneConstants;
Test class
public class Exec {
public static void Main (string[] args) {
Create a Form class variable
Demowindow DW = new Demowindow ("Network file download");
// Set the width and height of the form to 1/3 of the screen width and screen height, and the upper-left corner to the screen width and the screen height at 1/3
Toolkit Thekit = Dw.gettoolkit ();
Dimension wndsize = Thekit.getscreensize ();
Dw.setbounds ( WNDSIZE.WIDTH/3, WNDSIZE.HEIGHT/3, WNDSIZE.WIDTH/3,
WNDSIZE.HEIGHT/3);
Click the Close button to exit the program
Dw.setdefaultcloseoperation (Jframe.exit_on_close);
Set the form to visible
Dw.setvisible (TRUE);
}
}
Interface form
Class Demowindow extends JFrame implements ActionListener {
Enter a text box for the network file URL
JTextField JTF = new JTextField (25);
Action Button
JButton JB = new JButton ("Download");
Text area for displaying network file information
JTextArea JTA = new JTextArea ();
Set scroll bars for text areas
int v = scrollpaneconstants.vertical_scrollbar_as_needed;
int h = scrollpaneconstants.horizontal_scrollbar_as_needed;
JScrollPane jsp = new JScrollPane (JTA, V, h);
Layout panel
JPanel JP = new JPanel ();
Network File download
Downloader Downloader;
constructor function
Public Demowindow (String title) {
Super (title);
Form layout
Jp.setlayout (New FlowLayout (Flowlayout.left));
Jp.add (JTF);
Jp.add (JB);
Add (JP, Borderlayout.north);
Add (JSP, Borderlayout.center);
Adding event listeners
Jtf.addactionlistener (this);
Jb.addactionlistener (this);
}
Response Click button
public void actionperformed (ActionEvent e) {
Create a network file download class variable
Downloader = new Downloader (Jtf.gettext (), JTA);
Start the download thread
Thread thread = new Thread (downloader);
Thread.Start ();
}
}
Network File Download class
Class Downloader implements Runnable {
URL of the network file
String urlstring;
Text area for displaying network file information
JTextArea JTA;
//constructors
Public Downloader (String urlstring, JTextArea jta) {
//Set Properties
this.urlstring = urlstring;
this.jta = JTA;
}
Thread method for //download network files
public void Run () {
// Information about network files
StringBuffer info = new StringBuffer ();
try {
//URL of network file
url url = new URL (urlstring);
Open the URL connection for the network file
URLConnection urlconn = Url.openconnection ();
To add information about a network file
Info.append ("Host:" + url.gethost () + "/n");
Info.append ("Port:" + url.getdefaultport () + "/n");
Info.append ("Type of network file:" + urlconn.getcontenttype () + "/n");
Info.append ("Length:" + urlconn.getcontentlength () + "/n");
Info.append ("Downloading ...");
Display information about a network file
Jta.settext (Info.tostring ());
Create an input stream for a network file
InputStream is = Urlconn.getinputstream ();
Get the file name of a network file
String LocalFilename = Url.getfile (). SUBSTRING (
Url.getfile (). LastIndexOf ("/") + 1);
System.out.println (LocalFilename);
Http://www.baidu.com/index.html
Create a local file output stream
FileOutputStream fos = new FileOutputStream (localfilename);
Read network files to local files
int data;
while (data = Is.read ())! =-1) {
Fos.write (data);
}
Close the stream
Is.close ();
Fos.close ();
} catch (Exception e) {
System.out.println (E.getmessage ());
}
Jta.append ("Download complete. ");
}
}