How to read a file from file system in AEM | Community
Skip to main content
Rajeev_Kumar
New Participant
February 9, 2016
Solved

How to read a file from file system in AEM

  • February 9, 2016
  • 2 replies
  • 11575 views

Is there a way to read a file as Java.IO.File in AEM, because I need to pass a Java file object to a google api method and file is present on my computer file system. I tried various method like adapting a resource to File or getting file from BundleContext, but non of these worked for me.

Actually to get a GoogleCredential object I need to pass a service account email id, a json factory object and a .p12 file as Java.io.File object (which google provide for authentication).But I am not able to read the .p12 file from my file system or from project jar or or from content hierarchy.

Please suggest how can I read a file that is present on my file system in aem project.

Thanks in advance.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by kautuk_sahni

Hi 

Please red the following article:-

Link:- http://www.wemblog.com/2011/10/how-to-read-external-file-in-cq.html (create a custom component/service to read file from FileSystem or read it from JCR after bring it to JCR)

How to read an external file in CQ

Use Case: You want to read something from external file

Solution : There are multiple ways you can do this I will mentioned two of them here,

1) You can put them under /resources folder under /src/main for example if you want to read a file called "test.txt" then you can put file under /src/resources/<any-sub-foler>/<file-to-read>

Then use following code to read file 
InputStream stream = getClass().getResourceAsStream("<file-to-read>");
in above case it would be InputStream stream = getClass().getResourceAsStream("/test.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();

 

Another reference article :- https://helpx.adobe.com/experience-manager/using/uploading-files-aem1.html

//Uploading of images from FileSystem and keeping it in JCR.

 

I hope this would help you.

Thanks and Ragards

Kautuk Sahni

2 replies

New Participant
October 25, 2018

Hi

Still, we are not able to load the file which is present in the resources folder.
Name of the file geodb.mmdb.
Code for loading the file:

We are trying to load the file for adding Maxmind geoip2 solution.

AEM version : 6.3

Java version : 1.8

kautuk_sahni
kautuk_sahniAccepted solution
Employee
February 9, 2016

Hi 

Please red the following article:-

Link:- http://www.wemblog.com/2011/10/how-to-read-external-file-in-cq.html (create a custom component/service to read file from FileSystem or read it from JCR after bring it to JCR)

How to read an external file in CQ

Use Case: You want to read something from external file

Solution : There are multiple ways you can do this I will mentioned two of them here,

1) You can put them under /resources folder under /src/main for example if you want to read a file called "test.txt" then you can put file under /src/resources/<any-sub-foler>/<file-to-read>

Then use following code to read file 
InputStream stream = getClass().getResourceAsStream("<file-to-read>");
in above case it would be InputStream stream = getClass().getResourceAsStream("/test.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();

 

Another reference article :- https://helpx.adobe.com/experience-manager/using/uploading-files-aem1.html

//Uploading of images from FileSystem and keeping it in JCR.

 

I hope this would help you.

Thanks and Ragards

Kautuk Sahni

Kautuk Sahni