File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes Uploaded File Path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Uploaded File Path" Watch "Uploaded File Path" New topic
Author

Uploaded File Path

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

Volodymyr Levytskyi wrote:
Thank you for your help, and one more question:
How to find out root of my deployed web app programmatically except method servletContext.getContextPath and method servletContext.getRealPath?
I need to create new file via file.createNewfile() and I need absolute path to my web app because path like '/myRoot/webapp' doesn't work.


And here I just admonished you to ask new questions under their own separate topics.

I'll split this one off, too.


Customer surveys are for companies who didn't pay proper attention to begin with.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

Continuing...

DON'T use servletContext.getRealPath to attempt to get a filesystem path to write into. You shoulb never write into or modify your WAR. It may or may not succeed, and even if it does succeed, it almost certainly will come back and bite you someday.

Always upload files to a location that's external to both the webapp (WAR) and to the webapp server.
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

However, I don't recommend hard-coding this path into the webapp. What I normally do is feed it in as a webapp context variable. If I define the directory path in a Tomcat Context, I can use JNDI to retrieve it.

Can you explain this?


True person is moral, false is right!
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

I have this in one of my WEB-INF/web.xml files:


I can override the default path using a Tomcat Context element (other appservers have similar mechanisms)

The application can then retrieve this value using JNDI by looking up "java:comp/env/server/uploadDir".
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

I use this in my web.xml:

And only this full path works, anything else I tried failed.
In domain.xml I set this for a try:

Actually it didn't override env-entry from my web.xml as file is uploaded well to full path set in web.xml.

Thanks for all your replies!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Uploaded File Path