0
서블릿 프로그램이 있는데 (친구에게서 알게되었습니다) 콘솔에서 몇 가지 명령 프롬프트 명령을 실행하고 출력을 보류 중입니다. 이제는 여러 줄 콘솔 출력 줄을보고 싶습니다. 브라우저, 어떻게해야합니까? 당신이 브라우저에 쉘 명령의 출력을 인쇄 할 같은서블릿이 브라우저에 콘솔 값을 표시합니다.
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(), System.err)).start();
new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
PrintWriter stdin = new PrintWriter(p.getOutputStream());
stdin.println("source ./taxenv/bin/activate");
stdin.println("python runner.py");
// write any other commands you want here
stdin.close();
int returnCode = 0;
try {
returnCode = p.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
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_;
}
좋아, 내가 잘못한 것처럼 보였다. 코드로 표시 할 수 있습니까? –
내 대답을 편집했습니다. – pengfeil