Pages

Tuesday, 24 December 2013

Reading an external file in Adobe CQ/AEM

1) You can put them under /resources folder under /src/main for example 

If you want to read a file called "readme.txt" which was kept under /src/resources/, use below piece of code to achieve this task.

InputStream stream = getClass().getResourceAsStream("<file-to-read>");


Here, It could be:


InputStream stream = getClass().getResourceAsStream("/readme.txt");

Good practice is to follow convention, For example if your source code is under /src/main/java/com/test/mytest/Test.java Then your resource should be in location /resource/com/test/myTest/<Your File>

Then you can use something like:

InputStream in = Test.class.getResourceAsStream("<Your File>");


2) You can put file in repository (Any repository location is file) and use Node data as stream to read.

then use following code,
InputStream in = node.getNode("jcr:content").getProperty("jcr:data").getStream();



No comments:

Post a Comment

Converting InputStream to String

    private String convertToString(InputStream inputStreamObj)             throws IOException {         if (inputStreamObj != null) {     ...