You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jackrabbit.apache.org by Fernando Cabredo <fc...@yahoo.com> on 2009/07/28 23:02:23 UTC
Using InputStream
Hi,
I have the code below which lists files under a folder node and copy the files into my c:\ drive. However, I am not getting right the use of InputStream and FileOutputStream.this is why the file that is being copied into my c:\ is incorrect.
Can anybody please help me on this?
Thank you.
Fernando Cabredo
-----------------------------------------------------------------------------------------------------
String nodeType;
Node n;
Node root = session.getRootNode();
if(selectedNode.equals("")){
n = root;
}else{
selectedNode = selectedNode.substring(1, selectedNode.length());
n = root.getNode(selectedNode);
}
NodeIterator nodes = n.getNodes();
System.out.println("Listing Files of Folder Nodes under ---->"+ n.getName());
while (nodes.hasNext()) {
Node node;
node = nodes.nextNode();
String nodename = node.getName();
nodeType = node.getProperty("jcr:primaryType").getString();
if (nodeType.equals("nt:file")) {
System.out.println("Node name ----->"+nodename);
InputStream attachFileIS = null;
Node resNode = node.getNode("jcr:content");
Value attachFileValue = resNode.getProperty("jcr:data").getValue();
attachFileIS = attachFileValue.getStream();
fout = new FileOutputStream("c://"+nodename);
byte[] readData = new byte[1024];
int i = attachFileIS.read(readData);
while (i != -1) {
fout.write(i);
i = attachFileIS.read(readData);
}
fout.close();
}
//aList.add(nodeProperty);
}
}catch (Exception e) {
e.printStackTrace();
}
Re: Using InputStream
Posted by Alexander Klimetschek <ak...@day.com>.
On Tue, Jul 28, 2009 at 11:02 PM, Fernando Cabredo<fc...@yahoo.com> wrote:
> I have the code below which lists files under a folder node and copy the files into my c:\ drive. However, I am not getting right the use of InputStream and FileOutputStream.this is why the file that is being copied into my c:\ is incorrect.
> ...
> attachFileIS = attachFileValue.getStream();
> fout = new FileOutputStream("c://"+nodename);
> byte[] readData = new byte[1024];
> int i = attachFileIS.read(readData);
> while (i != -1) {
> fout.write(i);
> i = attachFileIS.read(readData);
> }
> fout.close();
I would suggest to use Apache commons-io [1] for those things. It has
IOUtils.copy() for copying from an Input to an OutputStream [2] (and
many other useful methods).
[1] http://commons.apache.org/io/
[2] http://commons.apache.org/io/api-1.4/org/apache/commons/io/IOUtils.html#copy(java.io.InputStream,%20java.io.OutputStream)
Regards,
Alex
--
Alexander Klimetschek
alexander.klimetschek@day.com
Re: Using InputStream
Posted by Stefan Guggisberg <st...@gmail.com>.
hi fernando
On Tue, Jul 28, 2009 at 11:02 PM, Fernando Cabredo<fc...@yahoo.com> wrote:
> Hi,
>
> I have the code below which lists files under a folder node and copy the files into my c:\ drive. However, I am not getting right the use of InputStream and FileOutputStream.this is why the file that is being copied into my c:\ is incorrect.
>
> Can anybody please help me on this?
>
> Thank you.
>
>
> Fernando Cabredo
>
>
> -----------------------------------------------------------------------------------------------------
> String nodeType;
> Node n;
> Node root = session.getRootNode();
> if(selectedNode.equals("")){
> n = root;
> }else{
> selectedNode = selectedNode.substring(1, selectedNode.length());
> n = root.getNode(selectedNode);
> }
> NodeIterator nodes = n.getNodes();
> System.out.println("Listing Files of Folder Nodes under ---->"+ n.getName());
> while (nodes.hasNext()) {
> Node node;
> node = nodes.nextNode();
> String nodename = node.getName();
> nodeType = node.getProperty("jcr:primaryType").getString();
> if (nodeType.equals("nt:file")) {
if (node.isNodeType("nt:file")) {
> System.out.println("Node name ----->"+nodename);
> InputStream attachFileIS = null;
> Node resNode = node.getNode("jcr:content");
> Value attachFileValue = resNode.getProperty("jcr:data").getValue();
> attachFileIS = attachFileValue.getStream();
> fout = new FileOutputStream("c://"+nodename);
> byte[] readData = new byte[1024];
int read;
while ((read = attachFileIS.read(readData)) > 0) {
fout.write(readData, 0, read);
}
attachFileIS.close();
fout.close();
> int i = attachFileIS.read(readData);
> while (i != -1) {
> fout.write(i);
> i = attachFileIS.read(readData);
> }
> fout.close();
> }
> //aList.add(nodeProperty);
> }
> }catch (Exception e) {
> e.printStackTrace();
> }
>
>
>
>
cheers
stefan