我试图通过响应对象来传输ProcessBuilder的输出。现在,只有在流程完成后,我才能在客户端获得输出。我希望看到客户端的输出被同时打印。目前,这是我的代码,并且在完成该过程后,它会在客户端(POSTMAN)中打印出所有内容。如何通过Jersey响应对象同时传输OutputStream
StreamingOutput stream = new StreamingOutput() {
@Override
public void write(OutputStream os) throws IOException, WebApplicationException {
String line;
Writer writer = new BufferedWriter(new OutputStreamWriter(os));
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
try {
while ((line = input.readLine()) != null) {
writer.write("TEST");
writer.write(line);
writer.flush();
os.flush();;
}
} finally {
os.close();
writer.close();
}
}
};
return Response.ok(stream).build();
看看这个https://dzone.com/articles/jerseyjax-rs-streaming-json – gladiator