任何人都可以就如何使一个REST API调用使用Java来启动一个可执行文件的建议? 假设我在本地的可执行文件,我想打一个REST API调用该可执行文件。
代码片断使用Servlet和JSP
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws
IOException {
try{
PrintWriter out = res.getWriter();
String browsefile = req.getParameter("browsefile");
if(browsefile == null || browsefile.equals("")){
out.println("File does not exist");
throw new ServletException("File Name can't be
null or empty");
}
File file = new File("C:/lic/test.li");
// File file = new File("C:/apache-tomcat-
7.0.42/webapps/Lic/test.li");
if(!file.exists()){
out.println("File does not exist");
throw new ServletException("File doesn't exists
on server.");
}
res.setContentType("text/html;charset=UTF-8");
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec
("C:\\tools\\server\\grd.exe" );
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 {
} }
}
JSP:
<form action="start" METHOD="GET" enctype="multipart/form-data">
<input type="file" name="browsefile" />
<input type="submit" name="start"
value="StartServer" />
</form>
<div id="result">
<pre>
${requestScope.startOutput}
</pre>
</div>
web.xml中:
<servlet>
<servlet-name>Start</servlet-name>
<servlet-class>com.abc.lic.Start</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Start</servlet-name>
<url-pattern>/start</url-pattern>
</servlet-mapping>