新人关于Java Ping IP 的程序遇到的问题,求各路大神解答

StarMoonStorm 2017-03-17 04:19:08
这个程序是设想在JtextArea上输入ip还有读取xls文件可以获取ip,然后循环处理批量的ip输出结果,而且可以保存。
现在的问题有:一,单ip输入到jtextarea之后可以判断出是否ping通,但多ip就出现两个ip都在一个数组;二,打开xls
文件之后ping出来的结果全是ping通,其中有一个不通的ip也显示ping通

我会在二楼放上程序代码。

控制台错误信息如下:

java.lang.ArrayIndexOutOfBoundsException: 1
at Ping.ipping(Ping.java:174)
at Ping.ping(Ping.java:96)
at Ping.buttonEvent(Ping.java:205)
at Ping$1.actionPerformed(Ping.java:44)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

...全文
110 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
StarMoonStorm 2017-03-17
  • 打赏
  • 举报
回复
引用 2 楼 aschouas 的回复:
这里说的很清楚 Line-174 越界
所以是怎么搞 Line 174是指 ipresult = ipAddress[i] + "\t" + "通" + "\r\n"; 代码里面的第178行
一个治疗术 2017-03-17
  • 打赏
  • 举报
回复


这里说的很清楚 Line-174 越界
StarMoonStorm 2017-03-17
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;

import javax.swing.*;

import org.apache.poi.hslf.model.Sheet;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;


public class Ping
{
	JTextArea getip = new JTextArea("" , 30 , 30);
	JTextArea result = new JTextArea("" , 30 , 30);
	JFileChooser choice = new JFileChooser();
	String bbuf = "";
	String cbuf;
	String cellValue1 = "";
	String cellValue2 = "";
	String ipresult = "";
	String[] ipAddress;
	File file ;
	Sheet sheet;
	int pingNum;
	boolean res = false;
	//界面布局
	public void show()
	{
		JFrame f = new JFrame("批量IP Ping");
		JPanel p3 = new JPanel();
		JButton check = new JButton("IP PING");
		JButton save = new JButton("保存结果");
		JButton fileOpen = new JButton("打开文件");
		JButton clean = new JButton("清空");
		check.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				buttonEvent(1);
			}
		});
		save.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				buttonEvent(2);
			}
		});
		fileOpen.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				buttonEvent(3);
			}
		});
		clean.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				buttonEvent(4);
			}
		});
		f.add(new JScrollPane(getip), BorderLayout.WEST);
		f.add(new JScrollPane(result), BorderLayout.EAST);
		p3.add(check);
		p3.add(save);
		p3.add(fileOpen);
		p3.add(clean);
		f.add(p3 , BorderLayout.CENTER);
		f.setSize(800, 600);	
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	//返回bbuf结果到ipping
	public void ping()
	{

        	if (pingNum == 1)
        	{
        		bbuf = cellValue1;
        		//ipping();
        		ipAddress = cellValue2.split("\r\n");
        		pingNum = 0;
        	}
        	else
        	{
        		bbuf = getip.getText();       
        		//ipping();
        		ipAddress = bbuf.split("\r\n");
        	}
        	ipping();
	}
	//获取表格信息
	public void getPoi()
	{
		   ArrayList<String> columnList = new ArrayList<String>(); 

			  try {  
			   FileInputStream in = new FileInputStream(file);  
			  
			   HSSFWorkbook wb = new HSSFWorkbook(in);  
			   String cellValue = "";
			   HSSFSheet sheet = wb.getSheetAt(0);  
			   //int firstRowNum = sheet.getFirstRowNum();  
			   int lastRowNum = sheet.getLastRowNum();  
			   Row row = null;  
			   Cell cell_a = null;  
			   //Cell cell_b = null;  
			   //获取第一列的信息
			   for (int i = 1; i <= lastRowNum; i++) 
			   {  
				    row = sheet.getRow(i);          //取得第i行  
				    cell_a = row.getCell(0);        //取得i行的第一列  
				   cellValue = cell_a.getStringCellValue().trim();   
				   cellValue2 = cellValue2 + cellValue + "\r\n";
				     columnList.add(cellValue);  
			    }  
			   //ipAddress = cellValue2.split("\r\n");
			   //System.out.println(ipAddress[0] + "\t" + ipAddress[1] + "\t" + ipAddress[2]);
			   getip.setText(cellValue2);
			   //获取第二列的信息
			   for (int j = 1; j <= lastRowNum; j++) 
			   {  
				    row = sheet.getRow(j);          //取得第j行  
				    cell_a = row.getCell(1);        //取得i行的第一列  
				   String cellValue3 = cell_a.getStringCellValue().trim();   
				   cellValue1 = cellValue1 + cellValue3 + "\r\n";
				     columnList.add(cellValue3);  
			   } 
			  } 
			  catch (Exception e) 
			  {  
			   e.printStackTrace();  
			  } 
	}
	//Ping ip并显示结果
	public void ipping()
	{		
        try
        {   	
        	BufferedReader reader = new BufferedReader(new StringReader(bbuf));
	        String oline = "";
	        String line = "";
	       	int i;
	       	String[] b;
	       	String dbuf = "";
	       	while((oline = reader.readLine()) != null)
	       	{
	        	dbuf = dbuf + oline + "\r\n";
	       	}
	        b = dbuf.split("\r\n");
		    Process pro = null;
		    cbuf = "";
		    for(i = 0 ; i < b.length ; i++)
		    {
		       	pro = Runtime.getRuntime().exec("ping " + " " + b[i]);
		        BufferedReader buf = new BufferedReader(new InputStreamReader
		               		(pro.getInputStream()));
		        while ((line = buf.readLine()) != null)
		        {
		        	System.out.println(line);
		            cbuf = cbuf + line + "\r\n";
		        }
		        if(cbuf.contains("TTL="))
	            {
		        	res = true;
		        	//break;
		        	//System.out.println("");
		        	ipresult = ipAddress[i] + "\t" + "通" + "\r\n";
	            }
		        else
	            {
	            	ipresult = ipAddress[i] + "\t" + "不通" + "\r\n";
	            } 
		    /*    if (res == true)
		        {
		        	ipresult = ipAddress[i] + "\t" + "通" + "\r\n";
		        }
		        else
	            {
	            	ipresult = ipAddress[i] + "\t" + "不通" + "\r\n";
	            } */
		       // ipresult = ipresult + ipAddress[i] ;
		       // System.out.println(ipcheck);
		        result.append(ipresult);
		        result.paintImmediately(result.getBounds());
		    }
        }
        catch (Exception e) 
		{  
		   e.printStackTrace();  
		} 
	}
	//按钮事件响应
	public void buttonEvent(int eventNum)
	{
		if (eventNum == 1)
		{
			result.setText("");
			ping();
		}
		if (eventNum == 2)
		{
			File ipresult = new File("c://ip result.txt");
			BufferedReader freader = new BufferedReader(new StringReader(cbuf));
			try
			(
					FileWriter tmpWri = new FileWriter(ipresult);	
			)
			{
				String fline = "";
				String fileString = "";
				while ((fline = freader.readLine()) != null)
				{
					fileString = fileString + fline + "\r\n";
				}
				tmpWri.write(fileString);
				result.setText("结果已保存到C://ip result.txt文件中");
			}
			catch(IOException ex)
			{
				ex.printStackTrace();
			}
		}
		if (eventNum == 3)
		{
			choice.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );   
			choice.showDialog(choice, "选择");
			file = choice.getSelectedFile();
			getPoi();
			pingNum = 1;
		}
		if (eventNum == 4)
		{
			getip.setText("");
			result.setText("");
			bbuf = "";
			cbuf = "";
			cellValue1 = "";
			cellValue2 = "";
			ipresult = "";
		}
	}
    public static void main(String args[])
    {
    	new Ping().show();	
    }
}

51,411

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧