package org.zzuli.xmsb; /** * 封装一个进程的信息。 * * @author 刘飞 * */ public class WindowsTask { /** * 映像名称 */ private String name; /** * PID */ private Integer pid; /** * 会话名 */ private String sessionName; /** * 会话编号 */ private Integer sessionId; /** * 内存使用 */ private long mem; @Override public String toString() { return "映像名称:\t" + name + ", PID:\t" + pid + ", 会话名:\t" + sessionName + ", 会话编号:\t" + sessionId + ", 内存使用:\t" + mem + " K\n"; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the pid */ public Integer getPid() { return pid; } /** * @param pid the pid to set */ public void setPid(Integer pid) { this.pid = pid; } /** * @return the sessionName */ public String getSessionName() { return sessionName; } /** * @param sessionName the sessionName to set */ public void setSessionName(String sessionName) { this.sessionName = sessionName; } /** * @return the sessionId */ public Integer getSessionId() { return sessionId; } /** * @param sessionId the sessionId to set */ public void setSessionId(Integer sessionId) { this.sessionId = sessionId; } /** * @return the mem */ public long getMem() { return mem; } /** * @param mem the mem to set */ public void setMem(long mem) { this.mem = mem; } }
package org.zzuli.xmsb; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Collection; import java.util.Hashtable; import java.util.Set; import java.util.Map.Entry; /** * 进程管理 * * @author 刘飞 * */ public class JavaWindowsTaskManager { private static final String X = " "; private static final String TSKILL_CMD = "tskill"; private static final String REPLACEMENT = ""; private static final String STRING = ","; private static final String K = "K"; private static final String UTF_8 = "UTF-8"; private static final String TASK_LIST_CMD = "taskList"; /** * 杀死一个进程 * * @param task * 进程号 * @throws IOException * */ public static void killTask(Integer pid) throws IOException { /** * 取得计算机所有进程任务列表 */ Hashtable<Integer, WindowsTask> tasks = getTaskList(); WindowsTask task = tasks.get(pid); if (task != null) { Runtime.getRuntime().exec(TSKILL_CMD + X + pid); } } /** * 杀死指定名称的所有进程 * * @param taskName * 进程服务名称 * @throws IOException */ public static void killTask(String taskName) throws IOException { Hashtable<Integer, WindowsTask> tasks = getTaskList(); Collection<WindowsTask> allTasks = tasks.values(); for (WindowsTask task : allTasks) { if (task.getName().equals(taskName)) { killTask(task.getPid()); } } } /** * 返回当前机器的所有进程 * * @return * @throws IOException */ public static Hashtable<Integer, WindowsTask> getTaskList() throws IOException { Hashtable<Integer, WindowsTask> tasks = new Hashtable<Integer, WindowsTask>(); Process process = Runtime.getRuntime().exec(TASK_LIST_CMD); InputStreamReader in = new InputStreamReader(process.getInputStream(), UTF_8); BufferedReader reader = new BufferedReader(in); String taskInfo = null; String tmp = null; while ((taskInfo = reader.readLine()) != null) { if (taskInfo.trim().length() > 0 && taskInfo.toUpperCase().indexOf(K) != -1) { WindowsTask task = new WindowsTask(); tmp = taskInfo.substring(0, 26); task.setName(tmp.trim()); tmp = taskInfo.substring(26, 35); task.setPid(Integer.valueOf(tmp.trim())); tmp = taskInfo.substring(35, 52); task.setSessionName(tmp.trim()); tmp = taskInfo.substring(52, 64); task.setSessionId(Integer.valueOf(tmp.trim())); tmp = taskInfo.substring(64, taskInfo.lastIndexOf(K)); tmp = tmp.replaceAll(STRING, REPLACEMENT); task.setMem(Long.valueOf(tmp.trim()).longValue()); tasks.put(task.getPid(), task); } } return tasks; } public static void main(String[] args) throws IOException { Hashtable<Integer, WindowsTask> tasks = getTaskList(); Set<Entry<Integer, WindowsTask>> taskEntry = tasks.entrySet(); for (Entry<Integer, WindowsTask> entry : taskEntry) { Integer key = entry.getKey(); WindowsTask value = entry.getValue(); System.out.println(key + " : " + value); if (value.getName().equals("WINWORD.EXE")) { killTask(value.getName()); } } } /** * 启动一个进程,传入的值可以是服务名,也可以是可执行文件的全路径名 * * @param task * @throws IOException */ public static void startTask(String task) throws IOException { Runtime.getRuntime().exec(task); } }
package org.zzuli.xmsb.service; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.zzuli.xmsb.JavaWindowsTaskManager; /** * Servlet implementation class for Servlet: JavaWindowsTaskManagerService * */ public class JavaWindowsTaskManagerService extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { private Logger logger = Logger.getLogger(JavaWindowsTaskManagerService.class.getName()) ; private static final String START = "start"; private static final String KILL = "kill"; private static final String NAME = "name"; private static final String STYLE = "style"; private static final long serialVersionUID = 1L; /* * (non-Java-doc) * * @see javax.servlet.http.HttpServlet#HttpServlet() */ public JavaWindowsTaskManagerService() { super(); } /* * (non-Java-doc) * * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, * HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /* * (non-Java-doc) * * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, * HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String style = request.getParameter(STYLE); String process_name = request.getParameter(NAME); logger.log(Level.ALL, style.equalsIgnoreCase(KILL) ? "杀死" : "启动" + "进程: " + process_name) ; if (style.equalsIgnoreCase(KILL)) { logger.log(Level.ALL, "杀死进程:" + process_name) ; try { JavaWindowsTaskManager.killTask(process_name); logger.log(Level.ALL, "成功杀死进程:" + process_name) ; } catch (Exception e) { logger.log(Level.ALL, "杀死进程:" + process_name + "异常\n" + e.getLocalizedMessage()) ; } } else if (style.equalsIgnoreCase(START)) { logger.log(Level.ALL, "启动进程:" + process_name) ; try { JavaWindowsTaskManager.startTask(process_name); logger.log(Level.ALL, "成功启动进程:" + process_name) ; } catch (Exception e) { logger.log(Level.ALL, "启动进程:" + process_name + "异常\n" + e.getLocalizedMessage()) ; } } else { // no cmd logger.log(Level.ALL, "无命令执行") ; } } }