从 JCR 文件节点获取文件

发布于 2024-10-12 02:29:09 字数 1043 浏览 2 评论 0原文

我有以下代码将“rose.gif”插入到roseNode 中。但是如何从存储库中检索该文件呢?

    Node roseNode = session.getRootNode().getNode("wiki:encyclopedia/wiki:entry[1]/");

    File file = new File("rose.gif");
    MimeTable mt = MimeTable.getDefaultTable();
    String mimeType = mt.getContentTypeFor(file.getName());
    if (mimeType == null) mimeType = "application/octet-stream";

    Node fileNode = roseNode.addNode(file.getName(), "nt:file");

    System.out.println( fileNode.getName() );

    Node resNode = fileNode.addNode("jcr:content", "nt:resource");
    resNode.setProperty("jcr:mimeType", mimeType);
    resNode.setProperty("jcr:encoding", "");
    resNode.setProperty("jcr:data", new FileInputStream(file));
    Calendar lastModified = Calendar.getInstance();
    lastModified.setTimeInMillis(file.lastModified());
    resNode.setProperty("jcr:lastModified", lastModified);

    //retrieve file and output as rose-out.gif
    File outputFile = new File("rose-out.gif");
    FileOutputStream out = new FileOutputStream(outputFile);

I have the following code to insert "rose.gif" into a roseNode. But how do I retrieve the file from the repository?

    Node roseNode = session.getRootNode().getNode("wiki:encyclopedia/wiki:entry[1]/");

    File file = new File("rose.gif");
    MimeTable mt = MimeTable.getDefaultTable();
    String mimeType = mt.getContentTypeFor(file.getName());
    if (mimeType == null) mimeType = "application/octet-stream";

    Node fileNode = roseNode.addNode(file.getName(), "nt:file");

    System.out.println( fileNode.getName() );

    Node resNode = fileNode.addNode("jcr:content", "nt:resource");
    resNode.setProperty("jcr:mimeType", mimeType);
    resNode.setProperty("jcr:encoding", "");
    resNode.setProperty("jcr:data", new FileInputStream(file));
    Calendar lastModified = Calendar.getInstance();
    lastModified.setTimeInMillis(file.lastModified());
    resNode.setProperty("jcr:lastModified", lastModified);

    //retrieve file and output as rose-out.gif
    File outputFile = new File("rose-out.gif");
    FileOutputStream out = new FileOutputStream(outputFile);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜尕妞 2024-10-19 02:29:09

您真正需要做的唯一一件事是从“nt:file”节点的名称获取文件的名称,并从“jcr:content”子节点上的“jcr:data”属性获取文件的内容。

JCR 1.0 和 2.0 在获取二进制“jcr:data”属性值的流方面略有不同。如果您使用 JCR 1.0,那么代码将如下所示:

Node fileNode = // find this somehow
Node jcrContent = fileNode.getNode("jcr:content");
String fileName = fileNode.getName();
InputStream content = jcrContent.getProperty("jcr:data").getStream();

如果您使用 JCR 2.0,最后一行有点不同,因为您首先必须从属性值获取 Binary 对象:

InputStream content = jcrContent.getProperty("jcr:data").getBinary().getStream();

然后您可以使用标准Java 流实用程序将“内容”流中的字节写入文件。

当您使用完 Binary 对象后,请务必调用 Binary 的 dispose() 方法来告诉信号您已经完成了 Binary 对象,并且该实现可以释放 Binary 获取的所有资源目的。您应该始终这样做,即使某些 JCR 实现尝试通过返回一个流来捕获编程错误,该流在关闭时会自动为您调用 dispose()

The only thing you really need to do is get the name of the file from the name of the "nt:file" node, and the content for the file from the "jcr:data" property on the "jcr:content" child node.

JCR 1.0 and 2.0 differ a bit in how you get the stream for the binary "jcr:data" property value. If you're using JCR 1.0, then the code would be like this:

Node fileNode = // find this somehow
Node jcrContent = fileNode.getNode("jcr:content");
String fileName = fileNode.getName();
InputStream content = jcrContent.getProperty("jcr:data").getStream();

If you're using JCR 2.0, the last line is a bit different because you first have to get the Binary object from the property value:

InputStream content = jcrContent.getProperty("jcr:data").getBinary().getStream();

You can then use standard Java stream utility to write the bytes from the 'content' stream into the file.

When you're done with the Binary object, be sure to call the Binary's dispose() method to tell signal that you're done with the Binary and that the implementation can release all resources acquired by the Binary object. You should always do this, even though some JCR implementations try to catch programming errors by returning a stream that, when closed, will automatically call dispose() for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文