您的位置:首页 > 编程语言 > Java开发

java实现windows下客户端软件的下载安装和检测

2013-10-15 11:25 459 查看
   最近我们有个客户端,需要在不同os系统上安装客户端程序并实时检测其进程是否OK,整体的设计思路,由jenkins的分布式调度策略来分发在不同机器上执行下载安装和检测程序,如图:

      



下面是java实现windows exe文件的下载、安装、进程检测。贴一段代码如下:

private  final static String OS_NAME="os.name";
private final static String WINDOWS_OS="windows";
private final static String LINUX_OS="linux";
private final static String windows_download_url="xxx";
private final static String linux_download_url="xxx";

private final static String window_check_pro_Ali_update="xxe.exe";

private final static String window_check_pro_AliYunDun="xxx.exe";

/**1、先判断是windows还是linux。
* 2、下载和安装。如果是windows。放入C盘 temp安装下,并执行安装程序,如果是linux,放入当前用户新建一个temp安装目录,并执行安装程序
* 3、检查进程是否起来,windows下采用的是tasklist(xxx.exe,等待一分钟,xxx.exe进程)。linux下采用的是top,断言进程是否存在(xxx 等待一分钟,xxxi)
*
*
* @param args
*/

/**
* os.name 	操作系统的名称
os.arch 作系统的架构
os.version  操作系统的版本
user.name 用户的账户名称
*/
String judge_os_type(){
String osName=null;
try{
osName=System.getProperty(OS_NAME);
if(null!=osName&&osName.indexOf(WINDOWS_OS)<0){
osName=WINDOWS_OS;
}else{
osName=LINUX_OS;
}
}catch (Exception e) {
System.out.println("error============"+e);
}

//取操作系统的相关信息放入数据库,错误信息详细定位
try {
InetAddress addr = InetAddress.getLocalHost();
String ip=addr.getHostAddress().toString();//获得本机IP
String  address=addr.getHostName().toString();//获得本机名称
//			System.out.println( "IP:"+ ip+",address:"+address);
}catch(Exception e) {
//				System.out.println("Bad IP Address!"+e);
}

return osName;
}

/**
* 下载并安装控件
* @param osType
* @throws IOException
*/
void download_install(String osType,String filedirPath) {
//下载安装程序
String exeNameAndpath=filedirPath+"\\xxx.exe";//下载文件存放路径
File dirFire=new File(filedirPath);
File exeFile=new File(exeNameAndpath);
try{
//判断文件夹是否存在和文件是否存在;
//如果文件存在,则删除

if(!dirFire.exists()){
dirFire.mkdir();
}
if(exeFile.exists()){
exeFile.delete();
}

URL url = new URL(windows_download_url);
URLConnection connect=url.openConnection();
InputStream in=connect.getInputStream();
FileOutputStream fos=new FileOutputStream(exeNameAndpath);
byte[] buffer=new byte[4*1024];
int read;
while ((read = in.read(buffer)) > 0) {
fos.write(buffer, 0, read);
}
fos.close();
in.close();
}catch(IOException e){
e.printStackTrace();
}
//安装程序

Runtime run=Runtime.getRuntime();
try{
Process pro=run.exec(exeNameAndpath);
System.out.println("安装完毕");
}catch (Exception e){
e.printStackTrace();
}
//pro.destroy();销毁开启的进程
//check一下 需要在前面sleep
try {
Thread.sleep(1000*10);
boolean ali_update_exit =check_pro_for_windows(window_check_pro_Ali_update);
if(ali_update_exit==true){
System.out.println(window_check_pro_Ali_update+" 该进程存在 ");
}else{
System.out.println(window_check_pro_Ali_update+" 该进程不存在 ");
}
Thread.sleep(1000*50);
boolean aliYunDun_exit=check_pro_for_windows(window_check_pro_AliYunDun);

if(aliYunDun_exit==true){
System.out.println(window_check_pro_AliYunDun+" 该进程存在 ");
}else{
System.out.println(window_check_pro_AliYunDun+" 该进程不存在 ");
}

} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

/**
* 根据进程名检查进程是否存在
* @return
*/
boolean check_pro_for_windows(String checkProcessName){
boolean exit=false;
Process process=null;
try{
//利用tasklist查看启用的进程
process=Runtime.getRuntime().exec("cmd.exe /c tasklist");
BufferedReader input=new  BufferedReader(new InputStreamReader(process.getInputStream()));
String line="";
while((line=input.readLine())!=null){
if(true==line.contains(checkProcessName)){
exit=true;
break;
}
System.out.println(line);
}
input.close();
}catch (Exception e) {
exit=false;
e.printStackTrace();
}
return exit;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐