我正在尝试将文件从我的Box.com帐户同步到AEM(CQ5) DAM。我已经编写了一个服务,在其中我可以向Box.com进行身份验证并获得文件。但是为了让我将这些文件上传到AEM DAM,我需要InputStream格式的文件。在Box.com文档(https://github.com/box/box-java-sdk/blob/master/doc/files.md)上,我找到了用于下载文件的代码片段。
BoxFile file = new BoxFile(api, "id");
BoxFile.Info info = file.getInfo();
FileOutputStream stream = new FileOutputStream(info.getName());
file.download(stream);
stream.close();但是我找不到任何我可以在Inputstream中获取文件的地方,以便我可以使用它将其上传到AEM DAM中。当我尝试从OutputStream转换到Inputstream时,在AEM中创建零字节文件并不是真的有效。
非常感谢您的指点和帮助!
提前谢谢。
发布于 2015-03-24 22:58:01
当我尝试在CQ中创建CSV并将其存储在JCR中时,我遇到了类似的问题。解决方案是管道流:
final PipedInputStream pis = new PipedInputStream();
final PipedOutputStream pos = new PipedOutputStream(pis);要真正写入JCR,您需要ValueFactory,它可以从JCR会话中获得(这里是我的CSV的示例):
ValueFactory valueFactory = session.getValueFactory();
Node fileNode = logNode.addNode("log.csv", "nt:file");
Node resNode = fileNode.addNode("jcr:content", "nt:resource");
resNode.setProperty("jcr:mimeType", "text/plain");
resNode.setProperty("jcr:data", valueFactory.createBinary(pis));
session.save();编辑:使用BoxFile的未测试示例:
try {
AssetManager assetManager = resourceResolver.adaptTo(AssetManager.class);
BoxFile file = new BoxFile(api, "id");
BoxFile.Info info = file.getInfo();
final PipedInputStream pis = new PipedInputStream();
final PipedOutputStream pos = new PipedOutputStream(pis);
Executors.newSingleThreadExecutor().submit(new Runnable() {
@Override
public void run() {
file.download(pos);
}
});
Asset asset = assetManager.createAsset(info.getName(), pis, info.getMimeType(), true);
IOUtils.closeQuietly(pos);
IOUtils.closeQuietly(pis);
} catch (IOException e) {
LOGGER.error("could not download file: ", e);
}发布于 2015-03-30 20:14:28
如果我正确理解了代码,您正在将文件下载到名为info.getName()的文件中。尝试使用FileInputStream(info.getName())从下载的文件中获取输入流。
BoxFile file = new BoxFile(api, "id");
BoxFile.Info info = file.getInfo();
FileOutputStream stream = new FileOutputStream(info.getName());
file.download(stream);
stream.close();
InputStream inStream=new FileInputStream(info.getName());https://stackoverflow.com/questions/29235201
复制相似问题