Apache Jackrabbit 向仓库存入二进制例子。。

研究一天多了,终于有些成果,写下代码。继续弄lock和version问题

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Calendar; import javax.jcr.Node; import javax.jcr.NodeIterator; import javax.jcr.Repository; import javax.jcr.RepositoryException; import javax.jcr.Session; import javax.jcr.SimpleCredentials; import org.apache.jackrabbit.core.TransientRepository; import sun.net.www.MimeTable; import com.jr.dao.PersonDAO; /** * jackRabbit 向仓库中写入二进制数据 * @author 李晗 */ public class Test { /** * * @param args command line arguments (ignored) * @throws Exception if an error occurs */ public static void main(String[] args) throws Exception { Repository repository = new TransientRepository(); Session session = repository.login(new SimpleCredentials("username","password".toCharArray())); Test dao = new Test(); File file = new File("c://123.jpg"); dao.insertBinary(session, file); dao.getNodeProperties(session,file.getName() ); } //存入二进制数据 public void insertBinary(Session session,File file) throws RepositoryException, FileNotFoundException{ Node n = session.getRootNode(); MimeTable mt = MimeTable.getDefaultTable(); String mimeType = mt.getContentTypeFor(file.getName()); if (mimeType == null) mimeType = "application/octet-stream"; Node fileNode = n.addNode(file.getName(), "nt:file"); 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); session.save(); } //获取指定节点下面所有property public void getNodeProperties(Session session,String nodeName) throws RepositoryException{ Node node = session.getRootNode(); NodeIterator ni = node.getNodes(); while(ni.hasNext()){ Node n = ni.nextNode(); NodeIterator ni1 = n.getNodes(); while(ni1.hasNext()){ Node n1 = ni1.nextNode(); if(n1.getName().equals("jcr:content")){ System.out.println(n1.getName()); System.out.println("lastModified:"+n1.getProperty("jcr:lastModified").getDate()); System.out.println("mimeType:"+n1.getProperty("jcr:mimeType").getString()); System.out.println("data:"+n1.getProperty("jcr:data").getStream()); } } } } }

你可能感兴趣的:(Apache Jackrabbit 向仓库存入二进制例子。。)