aspose file tools*
The moose likes Servlets and the fly likes Write file under /WEB-INF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Write file under /WEB-INF" Watch "Write file under /WEB-INF" New topic
Author

Write file under /WEB-INF

Adrien Ruffie
Ranch Hand

Joined: Jan 14, 2009
Posts: 90
Hello all,

I have a problem: I have a file under /WEB-INF/data/myfile.xml
But when I try to write in this file, a FileNotFound is throw by:

final FileOutputStream fos = new FileOutputStream(
new File(Thread.currentThread().getContextClassLoader()
.getResource("/").toString().replace("classes/", "data/"+instanceFileName));

I think the file isn't found because is protected no ? (If I copy/paste the file url returned
in my navigator, the file can be available)

Can I specify file can be written under my web app, with a parameter like (in web.xml):


<resource-ref>
<res-ref-name>security.xml</res-ref-name>
<res-type>java.io.File</res-type>
<res-auth>Container</res-auth>
</resource-ref>

Do you know a way to do that ?

Thank you


SCJP 5, SCDJWS 5
http://adrien-ruffie.blogspot.com
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

You can get the file path to the context from the ServletContext, provided it is not packaged as a WAR (not a good idea to make this assumption without checking) but you're right that the ClassLoader will not let you browse outside the entries on the classpath.
Adrien Ruffie
Ranch Hand

Joined: Jan 14, 2009
Posts: 90
Yes I can found the file path to the context but if I try file.exist() the function return false to me ... The URI is correct but it seems, that it seems invisible.
I can also retrieve the path by ClassLoader but same problem occurred ...
Do you to a method like ServletContext.getResourceAsStream, but returning an outputstream of my file instead inputstream ?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

try System.out.println(context.getResource("/")) then start from there
Adrien Ruffie
Ranch Hand

Joined: Jan 14, 2009
Posts: 90
by: System.out.println(context.getResource("/WEB-INF/data/"+instanceFileName));

jndi:/localhost/myapp/WEB-INF/data/security.xml
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Write file under /WEB-INF