通过jsp运行net view命令

时间:2012-02-08 19:54:52

标签: java jsp

以下代码通过.java文件成功运行,但如果我尝试通过.jsp文件运行它,则无法运行。

如果我删除了IP地址和域,它就会成功运行。

我包含<%@ page import =“java.io。*”%>在jsp页面上。任何帮助将不胜感激。谢谢。

String[] commands = new String[]{"cmd", "/c", "net", "view", "IPADDRESSHERE", "/domain:DOMAIN"};

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(commands);
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String strLine;

while ((strLine = input.readLine()) != null)   {    
System.out.println(strLine);
}

TestServlet.java

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public TestServlet() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    try {

    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec("net view SHARENAMEGOESHERE");
    pr.wait();
    BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

    BufferedWriter output = new BufferedWriter(new FileWriter("networklist.properties", false));

    String [] nextLine;
    String strLine;
    String copied;
    String newString = null;
    StringBuilder toFile = new StringBuilder();                 

    while ((strLine = input.readLine()) != null)   {

        out.println(strLine+"<br>");    

       if(strLine.indexOf("Print") != -1){

           copied = strLine;                   
           copied = copied.replaceAll("\\s+", "");                 
           nextLine = copied.split("Print");

           if(nextLine.length == 2){                       
               newString = nextLine[1] + "=" + nextLine[0]+"\n";
               toFile.append(newString); 
               out.println(newString+"<br>");
           }                   
       }
    }


    if(null != newString){
        output.write(toFile.toString());
    }

    input.close();
    output.close();

    } catch (Exception e){
        e.printStackTrace();
        out.println(e.getMessage());
    } finally {
        out.close();
    }


}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request,response);
}

}

2 个答案:

答案 0 :(得分:0)

不要在JSP中运行它。在HttpServlet中执行此操作,并在需要时从JSP调用它。由于这个确切的原因,JSP并不意味着在其中包含复杂的Java代码 - 很难调试和识别问题。

答案 1 :(得分:0)

好像你可能错过了一些导入声明。

还有一件事,你是否试图在屏幕上打印读取文件? 那么你应该使用out.println而不是System.out.println。 System.out.println将在控制台上打印,而不是在页面上打印。

希望这有帮助。

仍然我的建议也是相同的,你应该把这个代码移动到一个适当的类servlet或一些实用程序类。