Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Write file under /WEB-INF

 
Adrien Ruffie
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try System.out.println(context.getResource("/")) then start from there
 
Adrien Ruffie
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by: System.out.println(context.getResource("/WEB-INF/data/"+instanceFileName));

jndi:/localhost/myapp/WEB-INF/data/security.xml
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic