http://topic.csdn.net/t/20030501/22/1730651.html
我用这贴里的方法写到整个程序里为什么不行啊,而且还要考虑一种情况是TTL在传输中过期啊,我想把能够ping 通的ip截取出来,而且我的cmd是中文环境的~
我那个是批量ping 读取数组的,用了这个方法很多能够ping 通的ip都显示不出来了~
我用这贴里的方法写到整个程序里为什么不行啊,而且还要考虑一种情况是TTL在传输中过期啊,我想把能够ping 通的ip截取出来,而且我的cmd是中文环境的~
我那个是批量ping 读取数组的,用了这个方法很多能够ping 通的ip都显示不出来了~
pageEncoding="utf-8" import="java.sql.*" import="java.io.*" import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
<title>java中调用ping命令</title>
</head>
<body>
<div align="center">
<h2>Ping IP页面</h2></div>
<div style="font-size:12px;font-family:宋体">
<%!public static String toChinese(String strvalue)
{
try{
if(strvalue==null)
return null;
else
{
strvalue=new String(strvalue.getBytes("ISO-8859-1"),"utf-8");
return strvalue;
}
}catch(Exception e){
return null;
}
}
%>
<% Statement stmtOR;
Connection connOrac;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connOrac = DriverManager.getConnection("jdbc:mysql://localhost/ip?user=root&password=1314521&useUnicode=true&characterEncoding=utf8");
stmtOR=connOrac.createStatement();
System.out.println("ok1");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// request.setCharacterEncoding("utf-8");
// String cardno=new String(request.getParameter("cardno").getBytes("ISO-8859-1"),"utf-8"); String cardno=request.getParameter("cardno");
System.out.println(cardno);
String type=request.getParameter("type");
System.out.println(type);
if(cardno == null){
cardno = "";
}
if(type == null ){
type = "";
}
String sql = "select SUBSTRING(StartIPText,1,9),StartIPText ,EndIPText from iptable ";
if(!"".equals(cardno)){
sql += " where Country like '%"+cardno+"%' " ;
}
if(!"".equals(type)){
sql += " and Local = '"+type+"'GROUP BY SUBSTRING(StartIPText,1,9) HAVING COUNT(*)>1;";
}
System.out.println(sql);
ResultSet rs = stmtOR.executeQuery(sql);
int i=0;
String line=null;
InputStream is =null;
InputStreamReader isr=null;
BufferedReader br =null;
String[] ip=new String[10000]; //待Ping的地址
while (rs.next()) {
if(i <ip.length) {
ip[i] = rs.getString("StartIPText");
}
System.out.println(ip[0]);//这条语句可以正常显示出startip字段中数据。
Process process =null;
Runtime runtime = Runtime.getRuntime(); try
{
process =runtime.exec("ping "+ip[0]+" -n 1");
is = process.getInputStream();
isr=new InputStreamReader(is);
br =new BufferedReader(isr);
out.println("<pre>");
while( (line = br.readLine()) != null )
{
if(line.lastIndexOf( "TTL=")!=-1){
// out.println(line);
StringTokenizer st = new StringTokenizer(line);
// 按记录一条一条输出
while (st.hasMoreTokens()) {
String number = st.nextToken();
String name = st.nextToken();
String door = st.nextToken();
String number2 = st.nextToken();
String name2 = st.nextToken();
String door2 = st.nextToken();
out.println(name);
}}
out.flush();
}
//end of while
out.println("</pre>");
is.close();
isr.close();
br.close();
}//end of try
catch(IOException e )
{
out.println(e);
runtime.exit(1);
}
}
} catch (SQLException ex) {
System.out.println(ex);
}
%>
</div>
</body>
</html>
在servlet中一次跳转则存入request范围,否则放入session范围。另一个jsp通过el表达式拿