A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 刘芮铭 于 2012-8-28 17:55 编辑
  1. package myiphunter;
  2. import java.net.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. class GetAllIP
  7. {
  8. private Frame f;
  9. private TextField tf;
  10. private Button but;
  11. private TextArea ta;

  12. GetAllIP()
  13. {
  14. init();
  15. }
  16. public void init()
  17. {
  18. f = new Frame("迷你IP地址获取器 V.1.2.3.0版本");
  19. f.setBounds(300,100,600,500);
  20. f.setLayout(new FlowLayout());

  21. tf = new TextField(60);

  22. but = new Button("获取当前地址所有IP列表");

  23. ta = new TextArea(25,70);

  24. f.add(tf);
  25. f.add(but);
  26. f.add(ta);


  27. myEvent();
  28. f.setVisible(true);
  29. }

  30. private void myEvent()
  31. {

  32. tf.addKeyListener(new KeyAdapter()
  33. {
  34. public void keyPressed(KeyEvent e)
  35. {
  36. if(e.getKeyCode()==KeyEvent.VK_ENTER)
  37. getIP();
  38. }
  39. });


  40. but.addActionListener(new ActionListener()
  41. {
  42. public void actionPerformed(ActionEvent e)
  43. {
  44. getIP();

  45. }
  46. });

  47. f.addWindowListener(new WindowAdapter()
  48. {
  49. public void windowClosing(WindowEvent e)
  50. {
  51. System.exit(0);
  52. }
  53. });
  54. }
  55. private void getIP()
  56. {
  57. String hostName = tf.getText();
  58. InetAddress[] ias;
  59. try
  60. {
  61. ias = InetAddress.getAllByName(hostName);


  62. if(hostName!=null)
  63. {
  64. ta.setText("");
  65. ta.append("["+hostName+"]: "+"所有IP地址列表如下:"+"\r\n");
  66. for(InetAddress iad : ias)
  67. {
  68. ta.append(iad+"\r\n");
  69. }
  70. }
  71. }
  72. catch (Exception e)
  73. {
  74. }



  75. }


  76. }
  77. class IPDemo
  78. {
  79. public static void main(String[] args) throws UnknownHostException
  80. {
  81.          new GetAllIP();
  82. }
  83. }
复制代码
运行起来还是比较正常的,比如输入这样完整的域名:www.baidu.com 是可以正常获取IP的
http://www.baidu.com/index.php?tn=98012088_3_dg这样的地址,就获取不到IP,请问怎么样可以解决呢?如下图:

1.jpg (21.1 KB, 下载次数: 60)

1.jpg

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

3 个回复

倒序浏览
怎么没人回答啊。。。。。求大侠
回复 使用道具 举报
本帖最后由 唐见 于 2012-8-28 20:10 编辑

在程序中将http://www.baidu.com/index.php?tn=98012088_3_dg中/index及后面的过滤掉在获取所得ip地址看下
回复 使用道具 举报
把com后面的都切掉
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马