here a generic example:
public Node importFile(Node parent, File file) throws RepositoryException, IOException {
// add some other means to extract mime type from filename
String mimeType = "application/octet-stream";
Node fileNode = parent.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);
// this might not be usefull here
parent.save();
return fileNode;
}
I think it would be very useful if we had a list of JCR "recipes" somewhere (either docs or wiki.... or even in a JCR-enabled CMS, hint hint ;-)
JCR is a pretty verbose API and having code snippets like those make a huge difference for adoption.
WDYT?
-- Stefano.
