zoukankan      html  css  js  c++  java
  • java实现Windows系统进程查询及kill进程和重启进程(适用查看邮件等)

     1 /**
     2      * @desc 启动进程
     3      * @author zp
     4      * @date 2018-3-29
     5      */
     6     public static void startProc(String processName) { 
     7          log.info("启动应用程序:" + processName);  
     8          if (StringUtils.isNotBlank(processName)) {  
     9              try {  
    10                  Desktop.getDesktop().open(new File(processName));  
    11              } catch (Exception e) {  
    12                  e.printStackTrace();  
    13                  log.error("应用程序:" + processName + "不存在!");  
    14              }  
    15          }   
    16     }
    17     /**
    18      * @desc 杀死进程
    19      * @author zp
    20      * @throws IOException 
    21      * @date 2018-3-29
    22      */
    23     public static void killProc(String processName) throws IOException {  
    24         log.info("关闭应用程序:" + processName);  
    25         if (StringUtils.isNotBlank(processName)) {  
    26             executeCmd("taskkill /F /IM " + processName);  
    27         } 
    28     }
    29     /**
    30      * @desc 执行cmd命令 
    31      * @author zp
    32      * @date 2018-3-29
    33      */
    34     public static String executeCmd(String command) throws IOException {  
    35         log.info("Execute command : " + command);  
    36         Runtime runtime = Runtime.getRuntime();  
    37         Process process = runtime.exec("cmd /c " + command);  
    38         BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));  
    39         String line = null;  
    40         StringBuilder build = new StringBuilder();  
    41         while ((line = br.readLine()) != null) {  
    42             log.info(line);  
    43             build.append(line);  
    44         }  
    45         return build.toString();  
    46     }  
    47     /**
    48      * @desc 判断进程是否开启
    49      * @author zp
    50      * @date 2018-3-29
    51      */
    52     public static boolean findProcess(String processName) {
    53         BufferedReader bufferedReader = null;
    54         try {
    55             Process proc = Runtime.getRuntime().exec("tasklist -fi " + '"' + "imagename eq " + processName +'"');
    56             bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    57             String line = null;
    58             while ((line = bufferedReader.readLine()) != null) {
    59                 if (line.contains(processName)) {
    60                     return true;
    61                 }
    62             }
    63             return false;
    64         } catch (Exception ex) {
    65             ex.printStackTrace();
    66             return false;
    67         } finally {
    68             if (bufferedReader != null) {
    69                 try {
    70                     bufferedReader.close();
    71                 } catch (Exception ex) {}
    72             }
    73         }
    74     }
    75     
    76      //downfile = "D:\DownFile\DownFile.exe";
    77     String url = request.getParameter("downfile"); 
    78     String procName = url.substring(url.lastIndexOf("\")+1,url.length());
    79     boolean exist= findProcess(procName); 
    80     try {
    81         if (exist) { 
    82             // 存在,那么就先杀死该进程
    83             killProc(procName);
    84             // 在启动
    85             startProc(url);
    86         }else {
    87             startProc(url);
    88         }
    89     } catch (Exception e) {
    90         // TODO: handle exception
    91         log.error("重启/杀死提取程序失败。。。");  
    92     }
  • 相关阅读:
    程序员必定会爱上的10款软件(转)
    用代码来细说Csrf漏洞危害以及防御
    UPX源码分析——加壳篇
    从零开始学习渗透Node.js应用程序
    自己动手python打造渗透工具集
    国内国外最好的java开发论坛及站点 [转]
    运维无小事之一次导致数据丢失的小变更
    使用python及工具包进行简单的验证码识别
    浅析企业安全中账户安全 的重要性
    全世界最顶级黑客同时沸腾在DEF CON 25,是怎样一种体验?
  • 原文地址:https://www.cnblogs.com/xiaoyue1606bj/p/10977799.html
Copyright © 2011-2022 走看看