Hello.
I'm learning Jackrabbit and am running into a problem extracting files maybe
someone can point me to where I'm going wrong. I've checked the forum here
and didn't find anything (or I missed something) that really answered my
question. Every file I try to extract from the repository ends up being zero
bytes long.
I have the following code that is called from a loop that traverses a folder
structure. From what I can tell, the information is inserted properly. That
is, I can later traverse the repository and see all the nodes and
properties. Here is the traversal and import code that I'm using.
TRAVERSAL:
private static void importFolder(Node parentnode,
File directory) throws
RepositoryException,
IOException {
File[] direntries = directory.listFiles();
System.out.println(parentnode.getPath());
for (int i = 0; i < direntries.length; i++) {
File direntry = direntries[i];
if (direntry.isDirectory()) {
Node childnode =
parentnode.addNode(direntry.getName(), "nt:folder");
importFolder(childnode, direntry);
} else {
importFile(parentnode, direntry);
}
}
}
IMPORT:
public static void importFile(Node parentnode,
File file) throws RepositoryException,
IOException {
MimeTable mt = MimeTable.getDefaultTable();
String mimeType = mt.getContentTypeFor(file.getName());
if (mimeType == null)
mimeType = "application/octet-stream";
Node fileNode = parentnode.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);
System.out.println(fileNode.getPath());
}
I am using the following code to extract the file. As I mentioned before,
the problem is that the file I get out, while it has the correct filename,
always has a file size of zero bytes.
private static void ExtractFile(Session session, String nodePath, String
fileName) throws RepositoryException {
log.debug("In ExtractFile");
Node root = session.getRootNode();
Node pathNode = root.getNode(nodePath);
Node fileNode = pathNode.getNode(fileName);
Node content = fileNode.getNode("jcr:content");
String path = content.getPath();
log.debug("Path = " + path);
Binary bin =
session.getNode(path).getProperty("jcr:data").getBinary();
InputStream stream = bin.getStream();
File f=new File("C:\\TEMP\\"+fileName);
try {
OutputStream out=new FileOutputStream(f);
byte buf[]=new byte[1024];
int len = 0;
try {
while(stream.read(buf)>0)
out.write(buf,0,len);
out.close();
stream.close();
log.debug("\nFile is
created...................................");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
Any insights you can provide will be greatly appreciated.
-Greg
--
View this message in context:
http://jackrabbit.510166.n4.nabble.com/Learning-Jackrabbit-Extracted-files-are-length-zero-tp4658373.html
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.