RE: Unable to get the real path (tomcat/webapps/any folder) Liferay7.0 JSF

Abuzar Rana, modified 7 Years ago. New Member Posts: 4 Join Date: 9/19/18 Recent Posts

Hi everyone,

I'm unable to get the real path of tomcat webapps folder in Liferay 7.0 using JSF Portlet. Below is the code which I've tried from different ways
to get the path but this code returns the null value. 

Method 1:

String path = FacesContext.getCurrentInstance().getExternalContext().getRealPath(""); // return null

Method 2:

PortletRequest req = (PortletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
PortletContext pc = req.getPortletSession().getPortletContext();
String path = pc.getRealPath(".");  //Even I've given the exact folder name instead of dot but no luck.

How can I get the real path of tomcat webapps folder in Liferay 7.0 using JSF Portlet any suggestions? Thanks in Advance.

thumbnail
Kyle Joseph Stiemann, modified 7 Years ago. Liferay Master Posts: 760 Join Date: 1/14/13 Recent Posts
Moved to the Liferay Faces - English category.
thumbnail
Kyle Joseph Stiemann, modified 7 Years ago. Liferay Master Posts: 760 Join Date: 1/14/13 Recent Posts

Hi Abuzar,
The JavaDoc for PortletContext.getRealPath() states:

This method returns null if the portlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).

Liferay 7.0 handles WARs itself rather than deploying them to the application server, so if you take a look in the $TOMCAT_HOME/webapps folder, you’ll see that your WAR is not there. Instead it is in the  $LIFERAY_HOME/osgi/war folder. Since the file(s) you are looking for are being served from a WAR archive (rather than the file system or an exploded war), getRealPath() will always return null.

If you provide some more context about what you are trying to do (and perhaps an SSCCE), we might be able to give you a different solution.

- Kyle

Abuzar Rana, modified 7 Years ago. New Member Posts: 4 Join Date: 9/19/18 Recent Posts

Hi Kyle,

Thanks for the reply. I'm trying to keep (store) files into $TOMCAT_HOME/webapps/projectnamefolder/temp I've created folder manually inside webapps because I'm storing files into that folder. But as you mentioned above getRealPath() will always return null from this method. You said absolutely right in the above comment.


My problem is that I want to store files into temp folder if I create temp folder inside the project hierarchy then still I'm unable to find 
the path of project temp folder.
After store the files into temp folder I need to access these files from that folder as well.

What should I need to do for getting the path of temp folder even this temp folder is inside project hierarchy or inside webapps folder ?

 

Thanks in advance. 

thumbnail
Kyle Joseph Stiemann, modified 7 Years ago. Liferay Master Posts: 760 Join Date: 1/14/13 Recent Posts

Hi Abuzar,
I recently did a project where I needed to use a temporary folder to store some things on the file system. To create the temp folder I used:

<span class="hljs-keyword">Path</span> newTempDir&nbsp;= Files.createTempDirectory(<span class="hljs-string">"newTempDirPrefix"</span>);
<span class="hljs-keyword">File</span> newTempFile = new <span class="hljs-keyword">File</span>(newTempDir, <span class="hljs-string">"newTempFile.txt"</span>);

The above code creates a temporary folder in the location defined by the java.io.tmpdir system property.

But I could have also used the app server temp dir property (and that might be a better option):

File tempDir = (File) FacesContext<span class="hljs-preprocessor">.getCurrentInstance</span>()
    <span class="hljs-preprocessor">.getExternalContext</span>()<span class="hljs-preprocessor">.getApplicationMap</span>()<span class="hljs-preprocessor">.get</span>(ServletContext<span class="hljs-preprocessor">.TEMPDIR</span>)<span class="hljs-comment">;</span>
File newTempFile = new File(tempDir, <span class="hljs-string">"newTempFile.txt"</span>)<span class="hljs-comment">;</span>

Is there a reason that you cannot use one of these techniques?

For more information on creating and using temporary directories in web/portlet applications, check out this excellent StackOverflow post by BalusC: https://stackoverflow.com/questions/31255366/how-to-save-generated-file-temporarily-in-servlet-based-web-application. It's also worth checking out his related Q&A on getRealPath() (he essentially says that you should never use getRealPath()): https://stackoverflow.com/questions/12160639/what-does-servletcontext-getrealpath-mean-and-when-should-i-use-it.

- Kyle

thumbnail
David H Nebinger, modified 7 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts
Abuzar Rana:

I'm trying to keep (store) files into $TOMCAT_HOME/webapps/projectnamefolder/temp I've created folder manually inside webapps because I'm storing files into that folder.

This is completely discouraged and very much unsupported by app servers. The webapp is not meant to be yours to to modify, it is the purview of the application server itself.

Tomcat has always been known to be a little lax at its assertion of ownership on that folder allowing, for example, direct modification to files in the exploded war directory.

That said, you still shouldn't do it.