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

opencms at biberbeatz.de opencms at biberbeatz.de
Thu Oct 13 10:16:59 CEST 2005

Hi Alex,
perfect. Works fine.
Thanks for the reply.


Alexander Kandzior wrote:
> CmsObject#getFilesInFolder() returns CmsResource Objects not
> Objects.
> 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,
> Alex.
> Alexander Kandzior
> Alkacon Software - The OpenCms Experts
> http://www.alkacon.com
>>-----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
>>CmsObject cms = OpenCms.initCmsObject("Guest"); 
>>cms.loginUser("Admin", "admin"); List filesInFolder = 
>>CmsFile testFile = (CmsFile) filesInFolder.get(3); String 
>>fileName = testFile.getName(); int lengthOfContents = 
>>//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);
>>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,

More information about the opencms-dev mailing list