2015-10-12 82 views
0

我想测试使用Apache Jackrabbit保存和检索文件。我不确定当我以后无法恢复时它是否正在保存。以下是我的代码。我的问题是如何检索我保存的文件。Jackrabbit文件存储

@Test 
public void storeFile() throws Exception { 
    File file = getFile(); 
    FileInputStream fileInputStream = new FileInputStream(file); 

    Repository repository = new TransientRepository(); 
    Session session = repository.login( 
    new SimpleCredentials("username", "password".toCharArray())); 
    try { 
     logger.info("logged in as user '{}'", session.getUserID()); 
     Node root = session.getRootNode(); 

     // Store content 
     Node hello = root.addNode("userid_12"); 
     Node world = hello.addNode("files"); 


     logger.debug("setting fileInputStream"); 
     world.getSession().getValueFactory().createBinary(fileInputStream); 

     session.save(); 

     // Retrieve content 
     Node node = root.getNode("userid_12/files"); 
     logger.info(node.getPath()); 
     //how do I retrieve the file now? 
     } 

回答

1

刚刚做了类似的事情,并使用示例页面(http://wiki.apache.org/jackrabbit/ExamplesPage)来做到这一点。

你需要告诉兔崽子节点类型,并通过它的二进制数据和保存时,像这样的MIME类型:

Node DocNode = currentDocNode.addNode(doc.getFilename(),"nt:file"); 
    Node contentNode = DocNode.addNode("jcr:content","nt:resource"); 
    Binary binary = session.getValueFactory().createBinary(file.getInputStream()); 
    contentNode.setProperty("jcr:data",binary); 
    contentNode.setProperty("jcr:mimeType",file.getContentType()); 
    Calendar created = Calendar.getInstance(); 
    contentNode.setProperty("jcr:lastModified", created); 

这将正确保存在文档中的节点。然后检索文件刚开节点和读取数据时,有一个JcrUtils类,这是否对你:

Node fileNode = root.getNode("path/to/node"); 
InputStream stream = null; 
if(null != fileNode){ 
stream = JcrUtils.readFile(fileNode); 
} 

然后,你可以做你与InputStream的(流式传输到响应等)

什么
+0

感谢user2294467。 – Sonam

相关问题