当前情况:在 Java 代码中,我通过 Ektorp 库从 CouchDB 获取带有附件的文档。这些文档被映射到 Java 对象中,一切正常。为了使这些附件可以在浏览器中访问,我将一个 ByteArrayResource 实例化为一个字节数组、内容类型和文件名的文档附件:
private ByteArrayResource handleAttachment(String key, String cType) {
ByteArrayResource res = null;
AttachmentInputStream attIS = CouchDB.INSTANCE.getCouchDbConnector().getAttachment(doc.getId(), key);
InputStream is = new BufferedInputStream(attIS);
try {
// Convert InputStream to byte[] with Apache commons-io
byte[] bytes = IOUtils.toByteArray(is);
attIS.close();
is.close();
res = new ByteArrayResource(cType, bytes, key);
} catch (IOException e) {
logger.error("", e);
}
return res;
}
然后,我只需将 ResourceLink 添加到我的页面:
ByteArrayResource resource = handleAttachment(key, cType);
add(new ResourceLink("resLink", resource));
问题是:当我在浏览器中单击该链接时,所有附件都在下载,无论内容类型是什么。当我通过浏览器直接从 CouchDB 访问这些附件时,“image/xxx”内容类型会在浏览器中打开图像,“text/xxx”get 会显示在浏览器中,“并且还会处理“application/pdf”通过浏览器(例如 Safari 立即显示 PDF)。
我怎样才能用 Wicket 实现这一目标?任何帮助表示赞赏。请记住,我不想要共享资源,我的网站是安全的。谢谢!
PS:有趣的是,如果我使用“rel =”prettyPhoto”属性打开其中一个“图像”内容类型的资源链接,我会让 JQuery PrettyPhoto 插件在中途正确显示该图片。但是浏览器会触发下载。