⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 processcommand.java

📁 WAP PUSH后台源码,WAP PUSH后台源码
💻 JAVA
字号:
package com.sxit.wap.sys;

import java.io.*;
import java.util.*;
import java.net.*;
public class ProcessCommand {
    public ProcessCommand() {
    }
        public String getCommandExec(String cmd){
                String list = "";
                //cmd = "ping localhost";
                try {
                        //根据系统不同,动态的调用命令cmd 或者bash
                        Process proc = Runtime.getRuntime ().exec (cmd );
                        InputStream istr = proc.getInputStream ();
                        BufferedReader br =
                                new BufferedReader ( new InputStreamReader ( istr ) );
                        String str = "";
                        while ( ( str = br.readLine () ) != null )
                                list= list + str ;
                } catch (Exception e1) {
                        e1.printStackTrace();
                }
                return list.trim();
        }
        public String getCpuInfo() {
                return getCommandExec("top");
        }
        public String getDiskInfo() {
                return getCommandExec("disk");
        }

        public boolean isPingOk(String ip) {
          String list = "";
          int times = 2;
          while (times-- > 0) {
            try {
              list = getCommandExec("ping " + ip.trim());
              System.out.println(list);
              int position = list.indexOf("Lost = ");
              if (position > 0) {
                int position2 = list.indexOf("%", position);
                position = list.indexOf("(", position);
                int lostRate = Integer.parseInt(list.substring(position + 1,
                    position2));
                if (lostRate == 0)
                  break;
                else
                if (times == 0)
                  return false;
              }
              else
                return false;
            }
            catch (Exception e1) {
              e1.printStackTrace();
            }
          }
          return true;

        }
    public String isServerOk(String urlStr){
      try{
           URL url = new URL(urlStr);
           URLConnection urlConnection = url.openConnection();
           String ss = urlConnection.getHeaderField(0);
           if(ss.indexOf("500")>0){
             return "服务器页面错误";
           }
           DataInputStream in=new DataInputStream(url.openStream());
           String str = "";
           if((str=in.readLine())!=null){
           }
           in.close();
           return "服务正常";
      }catch(java.net.ConnectException e1){
        return "服务器的连接失败";
      }catch(java.io.FileNotFoundException e2){
        return "服务的请求应答失败";
      }catch(Exception ex){
        ex.printStackTrace();
        return ex.getMessage();
      }
    }
    public static void main(String[] args) {
                ProcessCommand processCommand = new ProcessCommand();

                System.out.println(processCommand.isServerOk("http://220.195.192.20/wap/index.jsp"));
               //System.out.println( processCommand.isPingOk("220.195.192.182") ? "OK":"failed");
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -