我正在尝试将图像从s3流式传输到客户端(通过StreamingOutput)并传输到缓存服务器。目前我有这样的想法:
final ByteArrayOutputStream cacheStream = new ByteArrayOutputStream(); //缓存
StreamingOutput stream = new StreamingOutput() { @Override public void write(OutputStream clientStream) throws IOException, WebApplicationException { OutputStream resultStream = new TeeOutputStream(clientStream, cacheStream); StreamUtils.copy(s3InputStream, resultStream); s3InputStream.close(); } };
我的问题:我不想在memery中保留任何内容,因此只需一次s3读取就可以流式传输到客户端和缓存中。 我知道TeeInputStream可以提供帮助,但无法弄清楚如何。感谢任何想法:)
更新:
对于那些感兴趣的人,
StreamingOutput stream = new StreamingOutput() {
@Override
public void write(OutputStream clientStream) throws IOException, WebApplicationException {
final InputStream wrappedInputStream = new TeeInputStream(s3InputStream, clientStream);
try {
//As soon as we write to cache, the respond also is streamed to client
cache.put(..., wrappedInputStream);
}
catch (Exception e) {
//Handle this
} finally {
s3InputStream.close();
wrappedInputStream.close();
}
}
};