【发布时间】:2012-01-05 00:57:35
【问题描述】:
我正在开发一个小型 Web 应用程序,它请求用户输入并将该输入作为服务器端机器上外部程序的命令行参数传递。
public class WorkflowServlet extends HttpServlet
public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
String username = request.getParameter( "username" );
String workflow = request.getParameter( "workflow" );
String preInflation = request.getParamater( "preInflation" );
String email = request.getParamater( "email" );
try {
executeShellCommand( "java ClusterProcess " + username + " "
+ workflow + " " + preInflation + " " + email );
} catch ( Exception e ) {
response.sendRedirect( "WorkflowAction.jsp" ); return;
}
response.sendRedirect( "WorkflowInProgress.jsp" );
}
}
public static void executeShellCommand( String command ) {
Runtime.getRuntime().exec( command.split( " " ) ).waitFor();
}
}
没有抛出异常 - 它似乎什么都不做。即使我传递了一些非常简单的东西,例如“touch test.txt”来执行ShellCommmand,它什么也不做。我可以通过命令行手动成功运行命令。
我必须做什么?
【问题讨论】:
-
另外,你为什么不使用waitFor的返回值呢?它可能会告诉你一些事情。
-
你能发布运行它的 URL 吗? (不,真的,不要 - 你没有逃避你的用户输入!!)
-
@Garee -- 使用 ProcessBuilder。它节省了空格处理的麻烦..命令可能在不同的工作目录(网络服务器的工作目录)中执行..尝试类似 /bin/touch /tmp/atestfile.txt
-
@davogotland,拆分将输入分成单独的参数。否则,在移交给操作系统时,它将被视为单个参数。
标签: java servlets runtime.exec