0

我将 java servlet 和 ektorp 用于 couchdb)。我将图像添加到数据库,但我没有显示它。

AttachmentInputStream data = db.getAttachment("document_id","attachment_id");

我通过这种方式获取附件。问题是我不知道如何在 java servlet 中显示这个获取的图像。

预先感谢。

4

1 回答 1

-2

基本上,您需要获取 HttpServletResponse 的输出流并进行缓冲写入。

String contentType = "image/png";
AttachmentInputStream data = db.getAttachment("document_id","attachment_id");

response.setContentType(contentType);
response.setContentLength(longToInt(data.getContentLength()));
OutputStream out = response.getOutputStream();
byte[] buffer = new byte[1024];
int count = 0;
while ((count = data.read(buffer)) >= 0) {
    out.write(buffer, 0, count);
}
out.close();
data.close();

从链接复制!

于 2016-07-28T11:31:37.537 回答