如何以编程方式编写nt:file [英] How to write an nt:file programmatically
问题描述
我正在尝试在动态创建的JCR中存储二进制数据。我的问题是,JCR API提供的唯一方法是通过 InputStream
:
会话会话= request.getResourceResolver()。adaptTo(Session.class);
ValueFactory valueFactory = session.getValueFactory();
Binary bin = valueFactory.createBinary(is);
由于CQ / Sling是RESTful,我可以理解为什么会这样,因为您通常会收到一份表格或对其他来源的httprequest,您总是可以使用 InputStream
。但就我而言,我正在即时创建二进制文件,通常表示为 OutputStream
。
- 在JCR API中还有其他方法可以忽略吗,在那里我可以直接在nt:file节点上创建
OutputStream
,就像FileOutputStream
? - 如果否,是否有一种简单的方法将
OutpuStream
转换为InputStream
?
我知道可以从Apache Commons获得另一种方法 IOUtils.copy()
。我在SO上看到了一些示例,这些示例仅使用 ByteArrayOutputStream.toByteArray()
创建 InputStream
。但是由于数据可能会变得很大,所以这不是一个好的解决方案。除了我尝试过,而且流还不完整,所以似乎有缓冲区限制。下一种方法是使用管道流,但是还有其他问题,我提出了另一个问题:多引号导致PipedOutputStream / OutputStreamWriter失败
编辑:
删除了PipedStream代码示例,因为我在另一个问题中发布了该问题的示例。所以在这里我仍然只是在寻找一种简单的方法来创建nt:file,其中输入不是 InputStream
。
管道在这里是很好的解决方案。但是,为了正确实现它们,您必须使用两个线程:第一个应将数据写入 PipedOutputStream
,第二个应创建 Binary
从 PipedInputStream
并将其保存到JCR:
最终PipedInputStream pis = new PipedInputStream();
final PipedOutputStream pos = new PipedOutputStream(pis);
Executors.newSingleThreadExecutor()。submit(new Runnable(){
@Override
public void run(){
try {
OutputStreamWriter writer = new OutputStreamWriter(pos );
writer.append(追加一些数据);
writer.close();
} catch(IOException e){
e.printStackTrace();
}
}
});
Binary二进制= session.getValueFactory()。createBinary(pis);
session.getNode( / content / myNode)。setProperty( xyz,二进制);
session.save();
在新线程中处理JCR的对称解决方案也很好。 p>
I'm trying to store binary data in JCR, which is created on the fly. My problem is that the only way provided by the JCR API is via an InputStream
:
Session session = request.getResourceResolver().adaptTo(Session.class);
ValueFactory valueFactory = session.getValueFactory();
Binary bin = valueFactory.createBinary(is);
As CQ/Sling is RESTful I can see why this is the case as you usually get a form post or an httprequest to another source, where you always have an InputStream
to use. But in my case I am creating the binary on the fly which usually is represented as an OutputStream
.
- Is there any other way I overlooked in the JCR API where I could create an
OutputStream
directly on the nt:file node, just like aFileOutputStream
? - If no, is there an easy way to have an
OutpuStream
transformed to anInputStream
?
I know the other way is available from the Apache Commons IOUtils.copy()
. I've seen some examples on SO where they just use the ByteArrayOutputStream.toByteArray()
to create an InputStream
. But as the data could get rather large, this is not a good solution. Besides I tried it and somehow the stream was incomplete, so it seems there is a buffer limmit. The next approach was with piped streams, but there I have other problems to which I opened another question: Multiple quotes cause PipedOutputStream/OutputStreamWriter to fail
EDIT:
Removed PipedStream code example as I posted the issue with it in another question. So here I am still just looking for an easy way to create an nt:file where the input is not an InputStream
.
Pipes are good solution here. However, in order to implement them properly, you have to use two threads: first should write data into the PipedOutputStream
and the second should create a Binary
from PipedInputStream
and save it into JCR:
final PipedInputStream pis = new PipedInputStream();
final PipedOutputStream pos = new PipedOutputStream(pis);
Executors.newSingleThreadExecutor().submit(new Runnable() {
@Override
public void run() {
try {
OutputStreamWriter writer = new OutputStreamWriter(pos);
writer.append("append here some data");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
Binary binary = session.getValueFactory().createBinary(pis);
session.getNode("/content/myNode").setProperty("xyz", binary);
session.save();
The symmetrical solution, in which you handle the JCR in the new thread would be also good.
这篇关于如何以编程方式编写nt:file的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!