DOC

3215.docjava

By Leo Palmer,2014-10-15 22:56
8 views 0
3215.docjava

    由于程序的原因(本文件未被完整保存。

    首页 ? 研究文集 ? I/O及网络

    网页读取技术 aminwoooo 转贴 (参与分;4004(专家分;40) 发表;2003-11-14 10:15 更新;2003-11-14 13:10 版本;

     1.0 阅读;3902

1. package netreader;

    2.

    3. import java.awt.*;

    4. import java.awt.event.*;

    5. import java.applet.*;

    6. import java.net.*;

    7. import java.io.*;

    8.

    9. /**

    10.* <p>Title: </p>

    11. * <p>Description: </p>

    12.* <p>Copyright: Copyright (c) 2003</p> 13.* <p>Company: </p>

    14.* @author not attributable

    15.* @version 1.0

    16.*/ 17.

    18.public class NetReader extends Applet { 19.private boolean isStandalone = false; 20.TextArea textArea1 = new TextArea(); 21.Button button1 = new Button();

    22.TextField textField1 = new TextField(); 23.//Get a parameter value

    24.public String getParameter(String key, String def) { 25.return isStandalone ? System.getProperty(key, def) : 26.(getParameter(key) != null ? getParameter(key) : def);

    27.}

    28.

    29.//Construct the applet

    30.public NetReader() {

    31.

    32.}

    33.//Initialize the applet

    34.public void init() {

    35.try {

    36.jbInit();

    37.}

38.catch(Exception e) {

    39.e.printStackTrace();

    40.}

    41.}

    42.//Component initialization

    43.private void jbInit() throws Exception { 44.textArea1.setText("textArea1");

    45.textArea1.setBounds(new Rectangle(1,37,294,261)); 46.button1.setLabel("get the web files"); 47.button1.setBounds(new Rectangle(295,30,2,2)); 48.button1.addActionListener(new java.awt.event.ActionListener()

    49.{

    50.public void actionPerformed(ActionEvent e) 51.{

    52.button1_actionPerformed(e);

    53.//button1_actionPerfomed

    54.}

    55.}

    56.

    57.

    58.);

    59.textField1.setText("please enter web address and file name");

    60.textField1.setBounds(new Rectangle(1,37,294,261)); 61.this.add(textArea1, null);

    62.this.add(button1, null);

    63.this.add(textField1, null);

    64.}

    65.public void ReadURL(String URLName) throws Exception 66.{

    67.int HttpResult;

    68.URL url=new URL(URLName);

    69.URLConnection urlconn=url.openConnection(); 70.urlconn.connect();

    71.HttpURLConnection httpconn=(HttpURLConnection)urlconn; 72.HttpResult=httpconn.getResponseCode(); 73.if(HttpResult!=HttpURLConnection.HTTP_OK) 74.{

    75.textArea1.setText("cant connct0");

    76.}

    77.else

    78.{

    79.int filesize=urlconn.getContentLength(); 80.InputStreamReader isReader=new InputStreamReader(urlconn.getInputStream());

    81.char[] buffer=new char[3000];

82.int num=0;

    83.while(num>-1)

    84.{

    85.num=isReader.read(buffer); 86.if(num<0)break;

    87.textArea1.append(new String(buffer,0,num));

    88.}

    89.

    90.isReader.close();

    91.

    92.}

    93.

    94.}

    95.void button1_actionPerformed(ActionEvent e)

    96.{

    97.String str=e.getActionCommand(); 98.try{

    99.textArea1.setText("");

    100. ReadURL(textField1.getText()); 101. }

    102. catch(Exception e1)

    103. {

    104. textArea1.setText("error:"+e1); 105. }

    106.

    107. }

    108.

    109. //Start the applet

    110. public void start() { 111. }

    112. //Stop the applet

    113. public void stop() { 114. }

    115. //Destroy the applet 116. public void destroy() { 117. }

    118. //Get Applet information 119. public String getAppletInfo() { 120. return "Applet Information"; 121. }

    122. //Get parameter info 123. public String[][] getParameterInfo() {

    124. return null;

    125. }

    126. //Main method

    127. public static void main(String[] args) {

    128. NetReader applet = new NetReader();

    129. applet.isStandalone = true;

    130. Frame frame;

    131. frame = new Frame();

    132. frame.setTitle("Applet Frame");

    133. frame.add(applet, BorderLayout.CENTER);

    134. applet.init();

    135. applet.start();

    136. frame.setSize(400,320);

    137. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

    138. frame.setLocation((d.width - frame.getSize().width) / 2, (d.height -

     frame.getSize().height) / 2);

    139. frame.setVisible(true);

    140. }

    141. }

    深入;用来读网页(读内部网没有问题上外部网需要代理。 如果网页嵌入applet(会出现

    java.security.AccessControlException: access denied (java.net.SocketPermission www.whu.edu.cn resolve)

    是因为Applet的沙漏安全控制禁止了对SocketPermission权限。Applet默认只能同自己宿主机器进 行通信。

版权声明 本篇文章对您是否有帮助, 投票; 投票结果;

    作者其它文章;

    作者全部文章 上一篇文章 下一篇文章 返回〔I/O及网络〕

Report this document

For any questions or suggestions please email
cust-service@docsford.com