从JCR文件节点获取文件 [英] Get File Out of JCR File Node

查看:187
本文介绍了从JCR文件节点获取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  Node roseNode = session.getRootNode()。getNode(wiki:encyclopedia /维基:条目[1] /); 

档案档案=新档案(rose.gif);
MimeTable mt = MimeTable.getDefaultTable();
String mimeType = mt.getContentTypeFor(file.getName());
if(mimeType == null)mimeType =application / octet-stream;

节点fileNode = roseNode.addNode(file.getName(),nt:file);

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

节点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);

//检索文件并输出为rose-out.gif
File outputFile = new File(rose-out.gif);
FileOutputStream out = new FileOutputStream(outputFile);


解决方案

nt:file节点名称中的文件名称,以及jcr:content子节点上的jcr:data属性中文件的内容。



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

  Node fileNode = //不知何故
Node jcrContent = fileNode.getNode(jcr:content);
String fileName = fileNode.getName();
InputStream content = jcrContent.getProperty(jcr:data)。getStream();

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

pre $ InputStream content = jcrContent.getProperty(jcr:data)。getBinary()。 getStream();

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

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


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);

解决方案

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.

这篇关于从JCR文件节点获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆