网页读取技术


package netreader;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import java.io.*;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class NetReader extends Applet {
private boolean isStandalone = false;
TextArea textArea1 = new TextArea();
Button button1 = new Button();
TextField textField1 = new TextField();
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}

//Construct the applet
public NetReader() {

}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
textArea1.setText("textArea1");
textArea1.setBounds(new Rectangle(1,37,294,261));
button1.setLabel("get the web files");
button1.setBounds(new Rectangle(295,30,2,2));
button1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
button1_actionPerformed(e);
//button1_actionPerfomed
}
}

);
textField1.setText("please enter web address and file name");
textField1.setBounds(new Rectangle(1,37,294,261));
this.add(textArea1, null);
this.add(button1, null);
this.add(textField1, null);
}
public void ReadURL(String URLName) throws Exception
{
int HttpResult;
URL url=new URL(URLName);
URLConnection urlconn=url.openConnection();
urlconn.connect();
HttpURLConnection httpconn=(HttpURLConnection)urlconn;
HttpResult=httpconn.getResponseCode();
if(HttpResult!=HttpURLConnection.HTTP_OK)
{
textArea1.setText("cant connct0");
}
else
{
int filesize=urlconn.getContentLength();
InputStreamReader isReader=new InputStreamReader(urlconn.getInputStream());
char[] buffer=new char[3000];
int num=0;
while(num>-1)
{
num=isReader.read(buffer);
if(num<0)break;
textArea1.append(new String(buffer,0,num));
}

isReader.close();

}

}
void button1_actionPerformed(ActionEvent e)
{
String str=e.getActionCommand();
try{
textArea1.setText("");
ReadURL(textField1.getText());
}
catch(Exception e1)
{
textArea1.setText("error:"+e1);
}

}

//Start the applet
public void start() {
}
//Stop the applet
public void stop() {
}
//Destroy the applet
public void destroy() {
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
//Main method
public static void main(String[] args) {
NetReader applet = new NetReader();
applet.isStandalone = true;
Frame frame;
frame = new Frame();
frame.setTitle("Applet Frame");
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
}

深入:用来读网页,读内部网没有问题上外部网需要代理。
如果网页嵌入applet,会出现
java.security.AccessControlException: access denied (java.net.SocketPermission www.whu.edu.cn resolve) 这是因为Applet的沙漏安全控制禁止了对SocketPermission权限。Applet默认只能同自己宿主机器进行通信。

本文作者:



相关阅读:
html小技巧的一些小技巧
使用PHP制作新闻系统的思路
asp xml 缓存类
网站优化 apache httpd最大连接数设置
css式样重用、子选择器和组选择器
IE CSS Bug及解决方案参考手册
删除及到期域名的查看(抢域名必备哦)
PHPUnit 3.0发布 集成Selenium组件和代码分析
jquery插件开发方法(初学者)
js 替换
Windows XP下锁定计算机的3种方法
Oracle数据库的Jobs创建和定时Jobs作业(工作) 的使用方法
mysql中char与varchar的区别分析
在LINUX桌面上启用Google小工具
快速导航

Copyright © 2016 phpStudy |