java实现的pint命令
来源:互联网 发布:python 字典 编辑:程序博客网 时间:2024/06/05 11:36
指定要测试的网址,用.properties的文件保存
add1=www.baidu.com
add2=www.sina.com
add3=www.163.com
web.xml如下配置
<!-- 监听指定的类 -->
<listener>
<listener-class>com.gangyi.listener.MyListener</listener-class>
</listener>
MyListener类如下
package com.gangyi.listener;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.gangyi.ping.Ping;
/***
* 服务器启动时进行监听
* @author ChenChao
* @createTime 2011-8-4
*/
public class MyListener implements ServletContextListener {
private Timer timer=null;
@Override
public void contextDestroyed(ServletContextEvent arg0) {
//注销timer
timer.cancel();
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
timer=new Timer(true);
timer.schedule(new TimerTask() {
@Override
public void run() {
//每隔3分钟指行指定方法
Ping.start();
}
},0,180000);
}
}
Ping类如下:
package com.gangyi.ping;
/**
* 用来测试指定网站的连接是否畅通,并将测试的信息写入文本中
* @author ChenChao
* @createTime 2011-8-4
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
public class Ping {
//定义要测试的地址
private static String add1;
private static String add2;
private static String add3;
public static void start() {
try {
//解析配置文件得到地址
Properties properties=new Properties();
properties.load(Ping.class.getResourceAsStream("/address.properties"));
add1=properties.get("add1").toString();
add2=properties.get("add2").toString();
add3=properties.get("add3").toString();
String[]address={add1,add2,add3};
//用来存放日志信息
String[]log={"e:\\"+add1+".txt","e:\\"+add2+".txt","e:\\"+add3+".txt"};
if(!new File("e:\\"+add1+".txt").exists()){
File file=new File(log[0]);
file.createNewFile();
File file2=new File(log[1]);
file2.createNewFile();
File file3=new File(log[2]);
file3.createNewFile();
}
for (int j = 0; j < address.length; j++) {
//执行ping指令
Process process = Runtime.getRuntime().exec("Ping " +address[j]);
// 对结果进行解析
InputStreamReader r = new InputStreamReader(process.getInputStream(), "GB2312");
LineNumberReader returnData = new LineNumberReader(r);
String line = "";
FileOutputStream fos=new FileOutputStream(new File(log[j]),true);
fos.write("=========================================".getBytes());
fos.write("\n".getBytes());
fos.write(new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(new Date()).getBytes());
while ((line = returnData.readLine()) != null) {
fos.write(line.getBytes());
fos.write("\n".getBytes());
}
fos.write("=========================================".getBytes());
}
} catch (Exception e) {
e.printStackTrace();
}
}
//本地测试方法
public static void main(String[] args) {
Ping.start();
}
}
add1=www.baidu.com
add2=www.sina.com
add3=www.163.com
web.xml如下配置
<!-- 监听指定的类 -->
<listener>
<listener-class>com.gangyi.listener.MyListener</listener-class>
</listener>
MyListener类如下
package com.gangyi.listener;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.gangyi.ping.Ping;
/***
* 服务器启动时进行监听
* @author ChenChao
* @createTime 2011-8-4
*/
public class MyListener implements ServletContextListener {
private Timer timer=null;
@Override
public void contextDestroyed(ServletContextEvent arg0) {
//注销timer
timer.cancel();
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
timer=new Timer(true);
timer.schedule(new TimerTask() {
@Override
public void run() {
//每隔3分钟指行指定方法
Ping.start();
}
},0,180000);
}
}
Ping类如下:
package com.gangyi.ping;
/**
* 用来测试指定网站的连接是否畅通,并将测试的信息写入文本中
* @author ChenChao
* @createTime 2011-8-4
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
public class Ping {
//定义要测试的地址
private static String add1;
private static String add2;
private static String add3;
public static void start() {
try {
//解析配置文件得到地址
Properties properties=new Properties();
properties.load(Ping.class.getResourceAsStream("/address.properties"));
add1=properties.get("add1").toString();
add2=properties.get("add2").toString();
add3=properties.get("add3").toString();
String[]address={add1,add2,add3};
//用来存放日志信息
String[]log={"e:\\"+add1+".txt","e:\\"+add2+".txt","e:\\"+add3+".txt"};
if(!new File("e:\\"+add1+".txt").exists()){
File file=new File(log[0]);
file.createNewFile();
File file2=new File(log[1]);
file2.createNewFile();
File file3=new File(log[2]);
file3.createNewFile();
}
for (int j = 0; j < address.length; j++) {
//执行ping指令
Process process = Runtime.getRuntime().exec("Ping " +address[j]);
// 对结果进行解析
InputStreamReader r = new InputStreamReader(process.getInputStream(), "GB2312");
LineNumberReader returnData = new LineNumberReader(r);
String line = "";
FileOutputStream fos=new FileOutputStream(new File(log[j]),true);
fos.write("=========================================".getBytes());
fos.write("\n".getBytes());
fos.write(new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(new Date()).getBytes());
while ((line = returnData.readLine()) != null) {
fos.write(line.getBytes());
fos.write("\n".getBytes());
}
fos.write("=========================================".getBytes());
}
} catch (Exception e) {
e.printStackTrace();
}
}
//本地测试方法
public static void main(String[] args) {
Ping.start();
}
}
- java实现的pint命令
- typedef int* PINT与#define PINT int*的区别
- #define PINT int * typedef int * PINT;的区别
- int* pInt = new int(5)和int* pInt = new int[5]的区别
- #define PINT int*与typedef int *SINT的区别.
- #define int *PINT和typedef int*PINT
- Java调用Linux命令的实现例子
- Java实现linux的“tail -f”命令
- java实现hadoop的cat命令
- JAVA 执行 dos/shell命令的实现
- java实现liunx,windows命令的调用
- 小心const修饰的经过重新定义的(比如LPBYTE、PINT)指针的陷阱
- #define PINT int * typedef int * SINT; 这两的区别及define与undefine用法
- Linux单向pint通
- java实现ping命令
- java 实现linux命令
- java实现ping命令
- java 实现 ping命令
- 【动态规划】最大m子段和
- 关于IplImage中widthstep的大小与width,nchannels等的关系的问题
- Caused by: org.apache.maven.artifact.resolver.ArtifactNotFoundException: System is offline
- 传值问题
- sh 事物
- java实现的pint命令
- 推荐10款非常优秀的HTML5开发工具
- SAP Direct Browsing URLs for Pentesting
- opencv处理视频颠倒问题
- 李彦宏:百度将成就移动互联网时代开发者
- 9.34.2.使用系统属性 using properties
- Android任务、进程、线程详解
- Bitmap & Texture2D 互转方法搜集
- 在Android上使用XML