我需要在記錄可用時進行流式傳輸。因此,我有StreamingOutput與澤西島不起作用。表現爲塊
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import javax.ws.rs.core.StreamingOutput;
import org.apache.log4j.Logger;
public class StreamingOutputImpl extends RetrieverResource implements StreamingOutput {
private static final Logger LOGGER = Logger.getLogger(StreamingOutputImpl.class);
private OutputStream os;
public StreamingOutputImpl(final RequestParameters parameters, final String basePath) {
super(parameters, basePath);
}
@Override
public void write(final OutputStream os) throws IOException {
this.os = os;
read();
}
@Override
public void writeRecord(final GenericRecord record) {
try {
Writer writer = new BufferedWriter(new OutputStreamWriter(os));
writer.write(record.toString());
writer.flush();
} catch (final IOException e) {
LOGGER.error("Error while write record " + record, e);
}
}
}
假設我有100條記錄,writeRecord()被調用100次。我只能在第100條記錄後看到瀏覽器中的輸出(帶有@GET和@POST),而不是在每條記錄之後。
有什麼建議嗎?