[opencms-dev] CmsFile.getContents() method problem: transform toString

Alexander Kandzior alex at opencms.org
Thu Oct 13 09:47:49 CEST 2005

CmsObject#getFilesInFolder() returns CmsResource Objects not CmsFile
Try it like this:

List filesInFolder = cms.getFilesInFolder("/sites/default/test");
CmsResource testRes = (CmsResource )filesInFolder.get(3);
CmsFile testFile = CmsFile.upgrade(testRes, cms);
String fileName = testFile.getName(); 
int lengthOfContents = testFile.getContents().length;

Best Regards,

Alexander Kandzior
Alkacon Software - The OpenCms Experts


> -----Original Message-----
> From: opencms-dev-bounces at opencms.org 
> [mailto:opencms-dev-bounces at opencms.org] On Behalf Of 
> opencms at biberbeatz.de
> Sent: Thursday, October 13, 2005 12:27 AM
> To: opencms-dev at opencms.org
> Subject: [opencms-dev] CmsFile.getContents() method problem: 
> transform toString
> Hallo List,
> I am developing a Java class that runs in the context of 
> OpenCms therefore I have access to the CmsObject. I want my 
> class to extract the content of a specific file. The file 
> contains plain text which I want to extract. I tried it with 
> different file types. Same result.
> See my code snip.
> The problem is that everythings works until I want to convert 
> the byte[] (is the return value of the getContents() method) 
> into String.
> The result of the transformation is an empty string.
> API: http://synyx.de/api/opencms6.0.0/org/opencms/file/CmsFile.html
> <snip>
> CmsObject cms = OpenCms.initCmsObject("Guest"); 
> cms.loginUser("Admin", "admin"); List filesInFolder = 
> cms.getFilesInFolder("/sites/default/test");
> CmsFile testFile = (CmsFile) filesInFolder.get(3); String 
> fileName = testFile.getName(); int lengthOfContents = 
> testFile.getContents().length;
> //returns an empty string, should be a string with the 
> contents of the file; also tried it without "UTF-8" encoding 
> String contentsInString = new String(testFile.getContents(), 
> "UTF-8"); //empty result System.out.println(contentsInString);
> </snip>
> I have no idea why it does not work. I transformed several 
> byte arrays to string in other programs with the same 
> solution without any problems.
> Maybe someone can help me or show me another solution to get 
> the content of a file.
> I cannot use the CmsJspActionElement in some way in a normal 
> class (not a jsp and no servlet) or is there any way?
> Thanks in advance,
> Jan
> _______________________________________________
> This mail is send to you from the opencms-dev mailing list To 
> change your list options, or to unsubscribe from the list, 
> please visit http://mail.opencms.org/mailman/listinfo/opencms-dev

More information about the opencms-dev mailing list