我有一个 .exe 文件,它通过单击 jsp 页面上的按钮来启动和停止。当我们单击 jsp 页面上的按钮时,.exe 文件将启动,并且其日志放置在 .txt 文件中。例如,我的 txt 文件位于 C:\Debug\logfile.txt。我的问题是当我单击启动时 .exe 文件正在启动,其数据显示在指定的日志文件中,但输出未打印在 jsp 页面上。如果停止 .exe 文件,日志会显示在指定的日志文件和 jsp 页面上。请帮助我解决这个问题。下面是停止和启动 .exe 文件的代码片段。
用于启动 exe 文件的 servlet:
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws
IOException {
try{
String browsefile = req.getParameter("browsefile");
if(browsefile == null || browsefile.equals("")){
throw new ServletException("File Name can't be
null or empty");
}
File file = new File("C:/lic/test.lic");
if(!file.exists()){
throw new ServletException("File doesn't exists
on server.");
}
res.setContentType("text/html;charset=UTF-8");
PrintWriter out = res.getWriter();
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("C:\\tools\\server\\grd.exe -l[+]
C:\\Debug\\logfile.txt" );
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(pr.getInputStream()));
BufferedReader input = new BufferedReader(stdInput);
String serverstarted="";
StringBuffer startOutput = new StringBuffer();
while((serverstarted = input.readLine()) !=
null){
startOutput.append
(serverstarted + "\n");
}
req.setAttribute("startOutput",
startOutput.toString());
req.getRequestDispatcher
("grdoptions.jsp").forward(req, res);
}catch (Throwable t)
{
t.printStackTrace();
}
finally {
} }
}
用于停止 .exe 文件的 Servlet:
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws
IOException {
try{
res.setContentType("text/html;charset=UTF-8");
PrintWriter out = res.getWriter();
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("cmd /c taskkill /F /IM grd.exe");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(pr.getInputStream()));
BufferedReader input = new BufferedReader(stdInput);
String stop="";
StringBuffer stopOutput = new StringBuffer();
while((stop = input.readLine()) != null){
//out.println(stop);
stopOutput.append(stop + "\n");
}
req.setAttribute("stopOutput",
stopOutput.toString());
req.getRequestDispatcher
("grdoptions.jsp").forward(req, res);
// out.println("Command has been stopped");
}catch (Throwable t)
{
t.printStackTrace();
}
finally {
} }
}
jsp代码:
<table cellspacing="5" cellpadding="1" border="0">
<tr>
<td>Start Server</td>
<td>
<div id="start">
<form action="start" METHOD="GET"
enctype="multipart/form-data">
<input type="file" name=browsefile size="150" />
<input type="submit" name="start"
value="StartServer" />
</form>
</div>
<div id="result">
<pre>
${requestScope.startOutput}
</pre>
</div>
</td>
</tr>
</table>
<BR>
<table cellspacing="5" cellpadding="1" border="0">
<tr>
<td>Stop Server</td>
<td>
<div id="stop">
<form action="stop" METHOD="GET">
<input type="submit" name="txtSubmit2"
id="txtSubmit" value="StopServer" />
</form>
</div>
<div id="result">
<pre>
${requestScope.stopOutput}
</pre>
</div>
</td>
</tr>
</table>
WEB.XML:
<servlet>
<servlet-name>Start</servlet-name>
<servlet-class>com.abc.def.lic.Start</servlet-class>
</servlet>
<servlet>
<servlet-name>Stop</servlet-name>
<servlet-class>com.abc.def.lic.Stop</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Start</servlet-name>
<url-pattern>/start</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Stop</servlet-name>
<url-pattern>/stop</url-pattern>
</servlet-mapping>
我发现正在使用 Process pr = rt.exec("C:\tools\server\grd.exe -l[+]
C:\Debug\logfile.txt" ); 启动 exe 文件并打印 logfile.txt 中的数据,这会阻止在 jsp 页面上打印输出。如果我删除 -l[+] C:\Debug\logfile.txt,它将在网页上打印输出。但我需要网页和 logfile.txt 上的输出。任何建议都可以使用。