May 3, 2020
Estimated Post Reading Time ~

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");

A 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 a file in the repository (Any repository location is a file) and use Node data as stream to read.

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


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.