aspose file tools*
The moose likes Servlets and the fly likes Need to access a file from inside web app Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Need to access a file from inside web app" Watch "Need to access a file from inside web app" New topic
Author

Need to access a file from inside web app

Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2061
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2061
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

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

Joined: Aug 30, 2007
Posts: 42
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

Joined: Aug 24, 2007
Posts: 62
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

Joined: Feb 26, 2005
Posts: 2061
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

Joined: Oct 26, 2006
Posts: 218
Are you getting any exceptions?

Can you post the stacktrace and the error message?
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2061
Amol Nayak
Ranch Hand

Joined: Oct 26, 2006
Posts: 218
Where have you kept your xml file?
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2061
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

Joined: Oct 26, 2006
Posts: 218
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

Joined: Feb 26, 2005
Posts: 2061
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

Joined: Oct 26, 2006
Posts: 218
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

Joined: Feb 26, 2005
Posts: 2061
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");
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need to access a file from inside web app