You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jackrabbit.apache.org by Michal Hybler <m....@email.cz> on 2007/04/20 11:47:26 UTC

import end export file from repository problem

I have problem with files stored in repository.

In first phase I import file into repository.

--------------------import-------------------------------------
Node childNode = null;
		Node parentNode = getNodeById(pol.getId());
		try {
			String name = file.getName();
			
			childNode = parentNode.addNode(name,"nt:unstructured");
			childNode.setProperty("owner", getUserName());
			childNode.addMixin("mix:referenceable");
			MimeTable mt = MimeTable.getDefaultTable();
			String mimeType = mt.getContentTypeFor(file.getName());
					
			if (mimeType == null || mimeType.equals("application/xml")) {
				if (mimeType == null) {
					mimeType = "null";
				}
				if(name.endsWith(".doc")){
					mimeType = "application/msword";
				}
				if(name.endsWith(".xls")){
					mimeType = "application/vnd.ms-excel";
				}
				if(name.endsWith(".ppt")){
					mimeType = "application/vnd.ms-powerpoint";
				}
				if (mimeType.equals("application/xml")) {
					mimeType = "text/xml";
				}
				if(mimeType.equals("null")){
					int lenght = name.length();
					mimeType = name.substring(lenght-3, lenght);
				}
			}
			
		//create the mandatory child node - jcr:content
	        Node resNode = childNode.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);
			
			
			saveSession();
---------------------------end import
-----------------------------------------------

afther that I would like to open this file by Runtime.getRuntime().exec

I get this file like this
---------------------------------------export-----------------------------------
 File tempDir = File.createTempFile("CMS temp", "", null);
		
		if (tempDir.delete()) {
			if (tempDir.mkdir()) {
				
				tempDir.deleteOnExit();
				
				//Long start = System.currentTimeMillis();
				File file = null;
				Node node = getNodeById(id); //my own method session.getNodeById(id)
				if (node == null) {
					return null;
				}
				//Long end = System.currentTimeMillis();
				Node resNode = getResNode(node);//my own like Node.getNode(jcr:content)
				if (hasProperty(resNode, "jcr:data")) {
					try {
						String name = getName(node);
						String data = resNode.getProperty("jcr:data").getValue().getString();
						long lastModified =
resNode.getProperty("jcr:lastModified").getValue().getLong();
						
						file = new
File(tempDir.getPath()+System.getProperty("file.separator")+name);
						file.setLastModified(lastModified);
						
						FileWriter writer = new FileWriter(file);
						writer.write(data, 0, data.length()-1);
						writer.close();
						file.deleteOnExit();
						
					} catch (ValueFormatException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (PathNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (RepositoryException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 			
				} 
				
				return file;
afther that i call

File file = cmsBI.getFile(pol.getId()); //the method for export - above
Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL
\""+file.getAbsolutePath()+"\"");

word even the excel from microsoft ofiice pack shows message like file is
damaged or bad format
where is the problem, someone can help me?
Thanks for your ideas
 Michal
					
-- 
View this message in context: http://www.nabble.com/import-end-export-file-from-repository-problem-tf3612072.html#a10094070
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.


Re: import end export file from repository problem

Posted by Michal Hybler <m....@email.cz>.
Thank it works excellently.

Stefan Guggisberg wrote:
> 
> hi michal,
> 
> the problem is that you're retrieving the binary contents of your file
> as a String,
> i.e.
> 
>>                                                 String data =
>> resNode.getProperty("jcr:data").getValue().getString();
>>                                                 long lastModified =
>> resNode.getProperty("jcr:lastModified").getValue().getLong();
>>
>>                                                 file = new
>> File(tempDir.getPath()+System.getProperty("file.separator")+name);
>>                                                
>> file.setLastModified(lastModified);
>>
>>                                                 FileWriter writer = new
>> FileWriter(file);
>>                                                 writer.write(data, 0,
>> data.length()-1);
>>                                                 writer.close();
> 
> try something like this instead:
> 
>             InputStream in = resNode.getProperty("jcr:data").getStream();
>             // spool stream to temp file
>             FileOutputStream out = new FileOutputStream(file);
>             try {
>                 byte[] buffer = new byte[8192];
>                 int read = 0;
>                 while ((read = in.read(buffer)) > 0) {
>                     out.write(buffer, 0, read);
>                 }
>             } finally {
>                 out.close();
>                 in.close();
>             }
> 
> cheers
> stefan
> 
> On 4/20/07, Michal Hybler <m....@email.cz> wrote:
>>
>> I have problem with files stored in repository.
>>
>> In first phase I import file into repository.
>>
>> --------------------import-------------------------------------
>> Node childNode = null;
>>                 Node parentNode = getNodeById(pol.getId());
>>                 try {
>>                         String name = file.getName();
>>
>>                         childNode =
>> parentNode.addNode(name,"nt:unstructured");
>>                         childNode.setProperty("owner", getUserName());
>>                         childNode.addMixin("mix:referenceable");
>>                         MimeTable mt = MimeTable.getDefaultTable();
>>                         String mimeType =
>> mt.getContentTypeFor(file.getName());
>>
>>                         if (mimeType == null ||
>> mimeType.equals("application/xml")) {
>>                                 if (mimeType == null) {
>>                                         mimeType = "null";
>>                                 }
>>                                 if(name.endsWith(".doc")){
>>                                         mimeType = "application/msword";
>>                                 }
>>                                 if(name.endsWith(".xls")){
>>                                         mimeType =
>> "application/vnd.ms-excel";
>>                                 }
>>                                 if(name.endsWith(".ppt")){
>>                                         mimeType =
>> "application/vnd.ms-powerpoint";
>>                                 }
>>                                 if (mimeType.equals("application/xml")) {
>>                                         mimeType = "text/xml";
>>                                 }
>>                                 if(mimeType.equals("null")){
>>                                         int lenght = name.length();
>>                                         mimeType =
>> name.substring(lenght-3, lenght);
>>                                 }
>>                         }
>>
>>                 //create the mandatory child node - jcr:content
>>                 Node resNode = childNode.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);
>>
>>
>>                         saveSession();
>> ---------------------------end import
>> -----------------------------------------------
>>
>> afther that I would like to open this file by Runtime.getRuntime().exec
>>
>> I get this file like this
>> ---------------------------------------export-----------------------------------
>>  File tempDir = File.createTempFile("CMS temp", "", null);
>>
>>                 if (tempDir.delete()) {
>>                         if (tempDir.mkdir()) {
>>
>>                                 tempDir.deleteOnExit();
>>
>>                                 //Long start =
>> System.currentTimeMillis();
>>                                 File file = null;
>>                                 Node node = getNodeById(id); //my own
>> method session.getNodeById(id)
>>                                 if (node == null) {
>>                                         return null;
>>                                 }
>>                                 //Long end = System.currentTimeMillis();
>>                                 Node resNode = getResNode(node);//my own
>> like Node.getNode(jcr:content)
>>                                 if (hasProperty(resNode, "jcr:data")) {
>>                                         try {
>>                                                 String name =
>> getName(node);
>>                                                 String data =
>> resNode.getProperty("jcr:data").getValue().getString();
>>                                                 long lastModified =
>> resNode.getProperty("jcr:lastModified").getValue().getLong();
>>
>>                                                 file = new
>> File(tempDir.getPath()+System.getProperty("file.separator")+name);
>>                                                
>> file.setLastModified(lastModified);
>>
>>                                                 FileWriter writer = new
>> FileWriter(file);
>>                                                 writer.write(data, 0,
>> data.length()-1);
>>                                                 writer.close();
>>                                                 file.deleteOnExit();
>>
>>                                         } catch (ValueFormatException e)
>> {
>>                                                 // TODO Auto-generated
>> catch block
>>                                                 e.printStackTrace();
>>                                         } catch (PathNotFoundException e)
>> {
>>                                                 // TODO Auto-generated
>> catch block
>>                                                 e.printStackTrace();
>>                                         } catch (RepositoryException e) {
>>                                                 // TODO Auto-generated
>> catch block
>>                                                 e.printStackTrace();
>>                                         }
>>                                 }
>>
>>                                 return file;
>> afther that i call
>>
>> File file = cmsBI.getFile(pol.getId()); //the method for export - above
>> Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL
>> \""+file.getAbsolutePath()+"\"");
>>
>> word even the excel from microsoft ofiice pack shows message like file is
>> damaged or bad format
>> where is the problem, someone can help me?
>> Thanks for your ideas
>>  Michal
>>
>> --
>> View this message in context:
>> http://www.nabble.com/import-end-export-file-from-repository-problem-tf3612072.html#a10094070
>> Sent from the Jackrabbit - Users mailing list archive at Nabble.com.
>>
>>
> 
> 

-- 
View this message in context: http://www.nabble.com/import-end-export-file-from-repository-problem-tf3612072.html#a10157703
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.


Re: import end export file from repository problem

Posted by Stefan Guggisberg <st...@gmail.com>.
hi michal,

the problem is that you're retrieving the binary contents of your file
as a String,
i.e.

>                                                 String data = resNode.getProperty("jcr:data").getValue().getString();
>                                                 long lastModified =
> resNode.getProperty("jcr:lastModified").getValue().getLong();
>
>                                                 file = new
> File(tempDir.getPath()+System.getProperty("file.separator")+name);
>                                                 file.setLastModified(lastModified);
>
>                                                 FileWriter writer = new FileWriter(file);
>                                                 writer.write(data, 0, data.length()-1);
>                                                 writer.close();

try something like this instead:

            InputStream in = resNode.getProperty("jcr:data").getStream();
            // spool stream to temp file
            FileOutputStream out = new FileOutputStream(file);
            try {
                byte[] buffer = new byte[8192];
                int read = 0;
                while ((read = in.read(buffer)) > 0) {
                    out.write(buffer, 0, read);
                }
            } finally {
                out.close();
                in.close();
            }

cheers
stefan

On 4/20/07, Michal Hybler <m....@email.cz> wrote:
>
> I have problem with files stored in repository.
>
> In first phase I import file into repository.
>
> --------------------import-------------------------------------
> Node childNode = null;
>                 Node parentNode = getNodeById(pol.getId());
>                 try {
>                         String name = file.getName();
>
>                         childNode = parentNode.addNode(name,"nt:unstructured");
>                         childNode.setProperty("owner", getUserName());
>                         childNode.addMixin("mix:referenceable");
>                         MimeTable mt = MimeTable.getDefaultTable();
>                         String mimeType = mt.getContentTypeFor(file.getName());
>
>                         if (mimeType == null || mimeType.equals("application/xml")) {
>                                 if (mimeType == null) {
>                                         mimeType = "null";
>                                 }
>                                 if(name.endsWith(".doc")){
>                                         mimeType = "application/msword";
>                                 }
>                                 if(name.endsWith(".xls")){
>                                         mimeType = "application/vnd.ms-excel";
>                                 }
>                                 if(name.endsWith(".ppt")){
>                                         mimeType = "application/vnd.ms-powerpoint";
>                                 }
>                                 if (mimeType.equals("application/xml")) {
>                                         mimeType = "text/xml";
>                                 }
>                                 if(mimeType.equals("null")){
>                                         int lenght = name.length();
>                                         mimeType = name.substring(lenght-3, lenght);
>                                 }
>                         }
>
>                 //create the mandatory child node - jcr:content
>                 Node resNode = childNode.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);
>
>
>                         saveSession();
> ---------------------------end import
> -----------------------------------------------
>
> afther that I would like to open this file by Runtime.getRuntime().exec
>
> I get this file like this
> ---------------------------------------export-----------------------------------
>  File tempDir = File.createTempFile("CMS temp", "", null);
>
>                 if (tempDir.delete()) {
>                         if (tempDir.mkdir()) {
>
>                                 tempDir.deleteOnExit();
>
>                                 //Long start = System.currentTimeMillis();
>                                 File file = null;
>                                 Node node = getNodeById(id); //my own method session.getNodeById(id)
>                                 if (node == null) {
>                                         return null;
>                                 }
>                                 //Long end = System.currentTimeMillis();
>                                 Node resNode = getResNode(node);//my own like Node.getNode(jcr:content)
>                                 if (hasProperty(resNode, "jcr:data")) {
>                                         try {
>                                                 String name = getName(node);
>                                                 String data = resNode.getProperty("jcr:data").getValue().getString();
>                                                 long lastModified =
> resNode.getProperty("jcr:lastModified").getValue().getLong();
>
>                                                 file = new
> File(tempDir.getPath()+System.getProperty("file.separator")+name);
>                                                 file.setLastModified(lastModified);
>
>                                                 FileWriter writer = new FileWriter(file);
>                                                 writer.write(data, 0, data.length()-1);
>                                                 writer.close();
>                                                 file.deleteOnExit();
>
>                                         } catch (ValueFormatException e) {
>                                                 // TODO Auto-generated catch block
>                                                 e.printStackTrace();
>                                         } catch (PathNotFoundException e) {
>                                                 // TODO Auto-generated catch block
>                                                 e.printStackTrace();
>                                         } catch (RepositoryException e) {
>                                                 // TODO Auto-generated catch block
>                                                 e.printStackTrace();
>                                         }
>                                 }
>
>                                 return file;
> afther that i call
>
> File file = cmsBI.getFile(pol.getId()); //the method for export - above
> Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL
> \""+file.getAbsolutePath()+"\"");
>
> word even the excel from microsoft ofiice pack shows message like file is
> damaged or bad format
> where is the problem, someone can help me?
> Thanks for your ideas
>  Michal
>
> --
> View this message in context: http://www.nabble.com/import-end-export-file-from-repository-problem-tf3612072.html#a10094070
> Sent from the Jackrabbit - Users mailing list archive at Nabble.com.
>
>