|
|
- 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默认只能同自己宿主机器进行通信。 |
|