中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > 网络
网页读取技术
作者:未知 时间:2005-07-24 21:19 出处:JR 责编:chinaitpower
              摘要:网页读取技术
  1. package netreader; 
  2. import java.awt.*; 
  3. import java.awt.event.*; 
  4. import java.applet.*; 
  5. import java.net.*; 
  6. import java.io.*; 
  7. /** 
  8. * <p>Title: </p> 
  9. * <p>Description: </p> 
  10. * <p>Copyright: Copyright (c) 2003</p> 
  11. * <p>Company: </p> 
  12. * @author not attributable 
  13. * @version 1.0 
  14. */ 
  15. public class NetReader extends Applet { 
  16. private boolean isStandalone = false
  17. TextArea textArea1 = new TextArea(); 
  18. Button button1 = new Button(); 
  19. TextField textField1 = new TextField(); 
  20. //Get a parameter value 
  21. public String getParameter(String key, String def) { 
  22. return isStandalone ? System.getProperty(key, def) : 
  23. (getParameter(key) != null ? getParameter(key) : def); 
  24. //Construct the applet 
  25. public NetReader() { 
  26. //Initialize the applet 
  27. public void init() { 
  28. try { 
  29. jbInit(); 
  30. catch(Exception e) { 
  31. e.printStackTrace(); 
  32. //Component initialization 
  33. private void jbInit() throws Exception { 
  34. textArea1.setText("textArea1"); 
  35. textArea1.setBounds(new Rectangle(1,37,294,261)); 
  36. button1.setLabel("get the web files"); 
  37. button1.setBounds(new Rectangle(295,30,2,2)); 
  38. button1.addActionListener(new java.awt.event.ActionListener() 
  39. public void actionPerformed(ActionEvent e) 
  40. button1_actionPerformed(e); 
  41. //button1_actionPerfomed 
  42. ); 
  43. textField1.setText("please enter web address and file name"); 
  44. textField1.setBounds(new Rectangle(1,37,294,261)); 
  45. this.add(textArea1, null); 
  46. this.add(button1, null); 
  47. this.add(textField1, null); 
  48. public void ReadURL(String URLName) throws Exception 
  49. int HttpResult; 
  50. URL url=new URL(URLName); 
  51. URLConnection urlconn=url.openConnection(); 
  52. urlconn.connect(); 
  53. HttpURLConnection httpconn=(HttpURLConnection)urlconn; 
  54. HttpResult=httpconn.getResponseCode(); 
  55. if(HttpResult!=HttpURLConnection.HTTP_OK) 
  56. textArea1.setText("cant connct0"); 
  57. else 
  58. int filesize=urlconn.getContentLength(); 
  59. InputStreamReader isReader=new InputStreamReader(urlconn.getInputStream()); 
  60. char[] buffer=new char[3000]; 
  61. int num=0; 
  62. while(num>-1) 
  63. num=isReader.read(buffer); 
  64. if(num<0)break
  65. textArea1.append(new String(buffer,0,num)); 
  66. isReader.close(); 
  67. void button1_actionPerformed(ActionEvent e) 
  68. String str=e.getActionCommand(); 
  69. try
  70. textArea1.setText(""); 
  71. ReadURL(textField1.getText()); 
  72. catch(Exception e1) 
  73. textArea1.setText("error:"+e1); 
  74. //Start the applet 
  75. public void start() { 
  76. //Stop the applet 
  77. public void stop() { 
  78. //Destroy the applet 
  79. public void destroy() { 
  80. //Get Applet information 
  81. public String getAppletInfo() { 
  82. return "Applet Information"
  83. //Get parameter info 
  84. public String[][] getParameterInfo() { 
  85. return null
  86. //Main method 
  87. public static void main(String[] args) { 
  88. NetReader applet = new NetReader(); 
  89. applet.isStandalone = true
  90. Frame frame; 
  91. frame = new Frame(); 
  92. frame.setTitle("Applet Frame"); 
  93. frame.add(applet, BorderLayout.CENTER); 
  94. applet.init(); 
  95. applet.start(); 
  96. frame.setSize(400,320); 
  97. Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); 
  98. frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2); 
  99. frame.setVisible(true); 
  100. }

深入:用来读网页,读内部网没有问题上外部网需要代理。 
如果网页嵌入applet,会出现 
java.security.AccessControlException: access denied (java.net.SocketPermission www.whu.edu.cn resolve) 这是因为Applet的沙漏安全控制禁止了对SocketPermission权限。Applet默认只能同自己宿主机器进行通信。
关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有