| Author |
Uploaded File Path
|
Tim Holloway
Saloon Keeper
Joined: Jun 25, 2001
Posts: 14491
|
|
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: 14491
|
|
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: 187
|
|
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?
|
 |
Tim Holloway
Saloon Keeper
Joined: Jun 25, 2001
Posts: 14491
|
|
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: 187
|
|
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!
|
 |
 |
|
|
subject: Uploaded File Path
|
|
|