• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need to access a file from inside web app

 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My java web app needs to parse in, an xml file. The app is on a web host server (not on my localhost), so I cannot do it like 'c:/xxxxx.xml'. Where can I put this file in my server, so that I can parse it in? And also, in which folder?

I have tried putting it in various folders of the web app but it is still not being seen by the pojo class trying to read it.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65128
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesus Angeles:
I have tried putting it in various folders of the web app but it is still not being seen by the pojo class trying to read it.
How are you trying to read it? Normal file I/o is problematic as you have seen.

Rather, use getResourceAsStream of the servlet context.
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am directly inputting it in the jaxp,

parser.parse("/marketplace_feed_v1.xml");

If I use the getResourceStream, where are the allowed places I can put the file, inside the web app?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65128
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesus Angeles:
If I use the getResourceStream, where are the allowed places I can put the file, inside the web app?
Anywhere within the app. Though I'd avoid the classpath folders.
 
Sylven Yip
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesus Angeles:
so I cannot do it like 'c:/xxxxx.xml'.


it's not quite right.
you can use:
HttpServlet.getServletContext().getRealPath("/xxxxx.xml");
or
request.getServletContext().getRealPath("/xxxxx.xml");

it returns the real path of the app.
in this way,you can use the file as local machine.
 
paritosh ranjan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be better to use getResourceAsStream() because getRealPath() can not be used when the file is inside a jar file.However,getResourceAsStream() or getResource().openStream() can be used even for files inside a jar file.
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The parse("") still cannot find the file, when I used the ResourceAsStream.

I put the input file, in the same level as WEB-INF, outside the WEB-INF.

I tried variations like:

parser.parse(sc.getResourceAsStream("/asdfsadf.xml"));

parser.parse(sc.getResourceAsStream("asdfsadf.xml"));

parser.parse(sc.getResourceAsStream(sc.getRealPath("asdfsadf.xml")));
[ September 28, 2007: Message edited by: Jesus Angeles ]
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you getting any exceptions?

Can you post the stacktrace and the error message?
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where have you kept your xml file?
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amol Nayak:
Where have you kept your xml file?


As I mentioned above,

I put the input file, in the same level as WEB-INF, outside the WEB-INF.
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you getting when you type this in your browser's address bar?

http://localhost:<portnum>/<appname>/asdfsadf.xml

Is this file served properly?
It is quiet possible you have misspelt the file name.
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amol Nayak:
What are you getting when you type this in your browser's address bar?

http://localhost:<portnum>/<appname>/asdfsadf.xml

Is this file served properly?
It is quiet possible you have misspelt the file name.


Got "The page cannot be found".
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope you have replaced <portnum> with a valid number as 8080 if you are using tomcat and <appname> with you own webapplication name. Assuming you have done that,the message shows that the file is not present in you app and thats why the resource is not found. Check the name of the file again, see if you have misspelt it.
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all, for the help.

It worked now. It included a misspelled filename.

This is what I used

parser.parse("http://localhost:9080/asdf/asdfasdf.xml");
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic