You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jackrabbit.apache.org by test123456 <sa...@thinkcoretech.com> on 2010/01/01 11:31:53 UTC

Storing and retrieving images from jackrabbit

Hi new to jackrabbit.

I try to create a content repository to store the  images uploaded by users
from my web application.

I am having the environment
Tomcat 6.0.13 server
JDK 1.6
Jackrabbit 1.6


I am using the following code snippet for that for adding image 

			
			Node rootNode = session.getRootNode();
			String fileName = "abc.gif";
			String destinationNodeName = "attachment";
			
			// First check the type of the file to add
			MimeTable mt = MimeTable.getDefaultTable();
			String mimeType = mt.getContentTypeFor(fileName);
			if (mimeType == null)
			{
				mimeType = "application/octet-stream";
			}
			
			// Get the destinationNode to which the file to be added
			Node destinationNode = rootNode.addNode(destinationNodeName);
			Node file = destinationNode.addNode(fileName, "nt:file");
			Node contentNode = file.addNode("jcr:content", "nt:resource");
			
			// Make a stream object using the file path
			InputStream fileStream = new FileInputStream(filePath);
			
			// set the mandatory properties
			contentNode.setProperty("jcr:data", fileStream);
			contentNode.setProperty("jcr:lastModified", Calendar.getInstance());
			contentNode.setProperty("jcr:mimeType", mimeType);
			
			session.save();
			System.out.println("File "+ fileName + "added to the Node "+
destinationNodeName + " in the repository");
			
			It is printing the message "File abc.gif added to the Node attachment in
the repository" in the console.

I used the following code snippet for diaplying image in jsp page
		
		
		
			
			Node rootNode = session.getRootNode();
			
			Striing sourceNodeName = "attachment";
			
			Node sourceNode = rootNode.getNode(sourceNodeName);
					
			if(sourceNode != null)
			{
				fileStream = sourceNode.getProperty("jcr:data").getStream(); // throws
javax.jcr.PathNotFoundException: jcr:data in this line
				
			}
				
				
		What will be reason for the exception.
		
I am attached my repository.xml 
		 http://n4.nabble.com/file/n991766/repository.xml repository.xml 
http://n4.nabble.com/file/n991766/repository.xml repository.xml 
-- 
View this message in context: http://n4.nabble.com/Storing-and-retrieving-images-from-jackrabbit-tp991766p991766.html
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.

Re: Storing and retrieving images from jackrabbit

Posted by Lance <la...@urbandiversion.com>.
So, they are both slightly wrong...


Solution:
Node fileNode = sourceNode.getNode(<FileName>);
Node contentNode = fileNode.getNode("jcf:content");
InputStream filestream = contentNode.getProperty("jcr:data").getStream();

**the middle node (content) was missing from the output that was in his scheme**

Lance

p.s. for you absolute beginners like me... 
here is how you output if it is an image...


response.setHeader("Content-Disposition", "inline;filename=\"" + 
            "photo.jpg" + "\"");
OutputStream out = response.getOutputStream();
response.setContentType("image/jpeg");
IOUtils.copy(ScapeImagesGenerationService.getImageStream(
        imageId.toString(), repository), out);
out.flush();




Re: Storing and retrieving images from jackrabbit

Posted by test123456 <sa...@thinkcoretech.com>.
Thank you very much.

I understood the problem. It create a node with the image file name.


On Sun, Jan 3, 2010 at 1:07 PM, vibhu7 [via Jackrabbit] <
ml-node+997613-1385984177@n4.nabble.com<ml...@n4.nabble.com>
> wrote:

> Hi,
>
> I think the problem lies in retrieval.
>
> your node addition code creates the following node structure
>
> root <-- attachment <-- 'filename' --- prop-'jcr:data'
>
> when you retrieve you check for
>
> root <-- attachment --- prop-'jcr:data'
>
> Solution:
> fileNode = sourceNode.getNode(<FileName>);
> fileNode.getProperty("jcr:data").getStream();
>
> Hope this solves the problem
>
> regards
> Vibhu
> projectoffguard.com
>
> -
>
> On Fri, Jan 1, 2010 at 4:01 PM, test123456 <[hidden email]<http://n4.nabble.com/user/SendEmail.jtp?type=node&node=997613&i=0>>wrote:
>
>
> >
> > Hi new to jackrabbit.
> >
> > I try to create a content repository to store the  images uploaded by
> users
> > from my web application.
> >
> > I am having the environment
> > Tomcat 6.0.13 server
> > JDK 1.6
> > Jackrabbit 1.6
> >
> >
> > I am using the following code snippet for that for adding image
> >
> >
> >                        Node rootNode = session.getRootNode();
> >                        String fileName = "abc.gif";
> >                        String destinationNodeName = "attachment";
> >
> >                        // First check the type of the file to add
> >                        MimeTable mt = MimeTable.getDefaultTable();
> >                        String mimeType = mt.getContentTypeFor(fileName);
> >                        if (mimeType == null)
> >                        {
> >                                mimeType = "application/octet-stream";
> >                        }
> >
> >                        // Get the destinationNode to which the file to be
>
> > added
> >                        Node destinationNode =
> > rootNode.addNode(destinationNodeName);
> >                        Node file = destinationNode.addNode(fileName,
> > "nt:file");
> >                        Node contentNode = file.addNode("jcr:content",
> > "nt:resource");
> >
> >                        // Make a stream object using the file path
> >                        InputStream fileStream = new
> > FileInputStream(filePath);
> >
> >                        // set the mandatory properties
> >                        contentNode.setProperty("jcr:data", fileStream);
> >                        contentNode.setProperty("jcr:lastModified",
> > Calendar.getInstance());
> >                        contentNode.setProperty("jcr:mimeType", mimeType);
>
> >
> >                        session.save();
> >                        System.out.println("File "+ fileName + "added to
> the
> > Node "+
> > destinationNodeName + " in the repository");
> >
> >                        It is printing the message "File abc.gif added to
> > the Node attachment in
> > the repository" in the console.
> >
> > I used the following code snippet for diaplying image in jsp page
> >
> >
> >
> >
> >                        Node rootNode = session.getRootNode();
> >
> >                        Striing sourceNodeName = "attachment";
> >
> >                        Node sourceNode =
> rootNode.getNode(sourceNodeName);
> >
> >                        if(sourceNode != null)
> >                        {
> >                                fileStream =
> > sourceNode.getProperty("jcr:data").getStream(); // throws
> > javax.jcr.PathNotFoundException: jcr:data in this line
> >
> >                        }
> >
> >
> >                What will be reason for the exception.
> >
> > I am attached my repository.xml
> >
> http://n4.nabble.com/file/n991766/repository.xmlrepository.xml
> > http://n4.nabble.com/file/n991766/repository.xml repository.xml
> > --
> > View this message in context:
> >
> http://n4.nabble.com/Storing-and-retrieving-images-from-jackrabbit-tp991766p991766.html
> > Sent from the Jackrabbit - Users mailing list archive at Nabble.com.
> >
>
>
> ------------------------------
>  View message @
> http://n4.nabble.com/Storing-and-retrieving-images-from-jackrabbit-tp991766p997613.html
> To unsubscribe from Storing and retrieving images from jackrabbit, click
> here< (link removed) =>.
>
>
>

-- 
View this message in context: http://n4.nabble.com/Storing-and-retrieving-images-from-jackrabbit-tp991766p998057.html
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.

Re: Storing and retrieving images from jackrabbit

Posted by Vibhu Sharma <vi...@projectoffguard.com>.
Hi,

I think the problem lies in retrieval.

your node addition code creates the following node structure

root <-- attachment <-- 'filename' --- prop-'jcr:data'

when you retrieve you check for

root <-- attachment --- prop-'jcr:data'

Solution:
fileNode = sourceNode.getNode(<FileName>);
fileNode.getProperty("jcr:data").getStream();

Hope this solves the problem

regards
Vibhu
projectoffguard.com

-

On Fri, Jan 1, 2010 at 4:01 PM, test123456 <sa...@thinkcoretech.com>wrote:

>
> Hi new to jackrabbit.
>
> I try to create a content repository to store the  images uploaded by users
> from my web application.
>
> I am having the environment
> Tomcat 6.0.13 server
> JDK 1.6
> Jackrabbit 1.6
>
>
> I am using the following code snippet for that for adding image
>
>
>                        Node rootNode = session.getRootNode();
>                        String fileName = "abc.gif";
>                        String destinationNodeName = "attachment";
>
>                        // First check the type of the file to add
>                        MimeTable mt = MimeTable.getDefaultTable();
>                        String mimeType = mt.getContentTypeFor(fileName);
>                        if (mimeType == null)
>                        {
>                                mimeType = "application/octet-stream";
>                        }
>
>                        // Get the destinationNode to which the file to be
> added
>                        Node destinationNode =
> rootNode.addNode(destinationNodeName);
>                        Node file = destinationNode.addNode(fileName,
> "nt:file");
>                        Node contentNode = file.addNode("jcr:content",
> "nt:resource");
>
>                        // Make a stream object using the file path
>                        InputStream fileStream = new
> FileInputStream(filePath);
>
>                        // set the mandatory properties
>                        contentNode.setProperty("jcr:data", fileStream);
>                        contentNode.setProperty("jcr:lastModified",
> Calendar.getInstance());
>                        contentNode.setProperty("jcr:mimeType", mimeType);
>
>                        session.save();
>                        System.out.println("File "+ fileName + "added to the
> Node "+
> destinationNodeName + " in the repository");
>
>                        It is printing the message "File abc.gif added to
> the Node attachment in
> the repository" in the console.
>
> I used the following code snippet for diaplying image in jsp page
>
>
>
>
>                        Node rootNode = session.getRootNode();
>
>                        Striing sourceNodeName = "attachment";
>
>                        Node sourceNode = rootNode.getNode(sourceNodeName);
>
>                        if(sourceNode != null)
>                        {
>                                fileStream =
> sourceNode.getProperty("jcr:data").getStream(); // throws
> javax.jcr.PathNotFoundException: jcr:data in this line
>
>                        }
>
>
>                What will be reason for the exception.
>
> I am attached my repository.xml
>                 http://n4.nabble.com/file/n991766/repository.xmlrepository.xml
> http://n4.nabble.com/file/n991766/repository.xml repository.xml
> --
> View this message in context:
> http://n4.nabble.com/Storing-and-retrieving-images-from-jackrabbit-tp991766p991766.html
> Sent from the Jackrabbit - Users mailing list archive at Nabble.com.
>