1

当前情况:在 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 插件在中途正确显示该图片。但是浏览器会触发下载。

4

2 回答 2

0

我看到您正在使用 ByteArrayResource 的构造函数版本,它还输入文件名(它是代码中的第三个参数“键”)。这样做 ByteArrayResource 会将响应配置为“附件”,这就是为什么您总是会看到保存对话框的原因。尝试省略 key 参数以查看是否获得所需的行为。

于 2012-08-05T13:43:25.150 回答
0

如果要保留文件名信息,可以尝试重写 ByteArrayResource 的 newResourceResponse 方法,如下所示:

res = new ByteArrayResource(cType, bytes, key){
  @Override
  protected AbstractResource.ResourceResponse newResourceResponse(IResource.Attributes attributes){
    AbstractResource.ResourceResponse rr = super.newResourceResponse(attributes)
    rr.setContentDisposition(ContentDisposition.INLINE);
    return rr;
  }
};

通过这种方式,您将手动强制您的响应为 INLINE。

于 2012-08-05T22:15:27.857 回答