demo1打印url網頁信息
package xunfang.com.web;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/**
* @description 打印url網頁信息
*/
public class WebBroswer2 {
public static void main(String[] args) {
try {
String urlString = "http://www.baidu.com";
InputStreamReader in = new InputStreamReader(
new URL(urlString).openStream());
StringBuffer input = new StringBuffer();
int ch;
while ((ch = in.read()) != -1)
input.append((char) ch);
String patternString = "<a\\s+href\\s*=\\s*(\"[^\"]*\"|[^\\s>]|\"[^\"]*\"\\s*+[^>\"]*\\s*=\\s*(\"[^\"]*\"|[^\\s>]))\\s*>";
Pattern pattern = Pattern.compile(patternString,
Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
int i = 0;
while (matcher.find()) {
int start = matcher.start();
int end = matcher.end();
String match = input.substring(start, end);
System.out.println(++i + " : " + match);
}
} catch (IOException exception) {
exception.printStackTrace();
} catch (PatternSyntaxException exception) {
exception.printStackTrace();
}
}
}
demo2輸入url打開網頁(會藍屏,排版有問題)
package xunfang.com.web;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
/**
* @description 輸入url打開網頁
* @author smalt
*
*/
public class Web2 extends JFrame implements ActionListener, Runnable {
private static final long serialVersionUID = 1L;
JButton button;
URL url;
JTextField text;
JEditorPane editPane;
byte b[] = new byte[118];
Thread thread;
public Web2() {
text = new JTextField("http://", 20);
editPane = new JEditorPane();
editPane.setEditable(false);
button = new JButton("確定");
button.addActionListener(this);
thread = new Thread(this);
JPanel p = new JPanel();
p.add(new JLabel("輸入網址"));
p.add(text);
p.add(button);
JScrollPane scroll = new JScrollPane(editPane);
add(scroll, BorderLayout.CENTER);
add(p, BorderLayout.NORTH);
setBounds(160, 60, 420, 300);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 設計窗口
editPane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
editPane.setPage(e.getURL());
} catch (IOException el) {
}
}
}
});// 處理超級鏈接
}
public void actionPerformed(ActionEvent e) {
if (!(thread.isAlive()))
thread = new Thread(this);
try {
thread.start();
} catch (Exception ee) {
text.setText("我正在讀取" + url);
}
}
public void run() {
try {
editPane.setText(null);
url = new URL(text.getText().trim());
editPane.setPage(url);// 打開頁面
} catch (Exception el) {
text.setText("" + el);
return;
}
}
public static void main(String[] args) {
Web2 application = new Web2();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
demo3調用系統瀏覽器打開網頁並截屏(有bug)
package xunfang.com.web;
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import javax.imageio.ImageIO;
//調用系統瀏覽器打開url地址並截屏
public class Web3 {
public static void main(String[] args) {
if (!java.awt.Desktop.isDesktopSupported()) {
System.err.println("Desktop is not supported (fatal)");
System.exit(1);
}
java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
if (!desktop.isSupported(java.awt.Desktop.Action.BROWSE)) {
System.err
.println("Desktop doesn't support the browse action (fatal)");
System.exit(1);
}
try {
URI uri = URI.create("http://www.baidu.com");
desktop.browse(uri);
Thread.sleep(8000); // 8 seconds is enough to load the any page.
Robot robot = new Robot();
// Rectangle rectangle = new
// Rectangle(Toolkit.getDefaultToolkit().getScreenSize() );
Rectangle rectangle = new Rectangle(300, 90, 1000, 720);
BufferedImage image = robot.createScreenCapture(rectangle);
File outputfile = new File("D:\\aa\\test.jpg");
ImageIO.write(image, "jpg", outputfile);
} catch (IOException e) {
e.printStackTrace();
} catch (AWTException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}