【发布时间】:2014-03-14 09:37:19
【问题描述】:
我正在通过我的 servlet 运行一些终端(或命令提示符)命令,如下所示
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String[] command =
{
"zsh"
};
Process p = Runtime.getRuntime().exec(command);
new Thread(new SyncPipe(p.getErrorStream(), response.getOutputStream())).start();
new Thread(new SyncPipe(p.getInputStream(), response.getOutputStream())).start();
PrintWriter stdin = new PrintWriter(p.getOutputStream());
stdin.println("source ./taxenv/bin/activate");
stdin.println("python runner.py");
stdin.close();
int returnCode = 0;
try {
returnCode = p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
} System.out.println("Return code = " + returnCode);
}
class SyncPipe implements Runnable
{
public SyncPipe(InputStream istrm, OutputStream ostrm) {
istrm_ = istrm;
ostrm_ = ostrm;
}
public void run() {
try
{
final byte[] buffer = new byte[1024];
for (int length = 0; (length = istrm_.read(buffer)) != -1; )
{
ostrm_.write(buffer, 0, length);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
private final OutputStream ostrm_;
private final InputStream istrm_;
}
这是在新窗口中执行终端命令后显示的结果,我想避免这种情况并将这些流值传递回 JSP 页面并在 div 中显示。
怎么做?
【问题讨论】:
-
不是
void,而是传回值并用JSP读入? -
而不是 'ostrm_.write(buffer, 0, length);'我想把它们发回 JSP
-
所以你建议我把所有这些东西放到 并导入东西??我会试试这个,并会更新你。
-
这取决于您的应用程序。请参考下面我的回答。
标签: java javascript html