This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Access file using servlets, provided file path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Access file using servlets, provided file path" Watch "Access file using servlets, provided file path" New topic
Author

Access file using servlets, provided file path

kavin clain
Ranch Hand

Joined: Jan 19, 2010
Posts: 68
How to access file saved in server using servlets. Actually the problem is how to specify file path?
Sharad Jadhav
Greenhorn

Joined: Feb 10, 2010
Posts: 18
String path = getServletContext () .getRealPath ("xyz.xml" ) ;
System.out.println ( path ) ;
File file = new File ( path ) ;

Hope this will help you
Cheers!!!


Java J2EE Hibernate Spring
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That helps if the file is relative to the application; if it's not, just access it like any other file, and stream it back to the client.
kavin clain
Ranch Hand

Joined: Jan 19, 2010
Posts: 68
Thanks for the code vijay... it is working.

David, could you please tell me how to get the file path if it is not relative to the application.

Thank you.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Just use the file path.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
kavin clain wrote:Thanks for the code vijay... it is working.

David, could you please tell me how to get the file path if it is not relative to the application.

Thank you.

He means if you know where the file is on your machine, and it's not contained within your web application.

You use one method to access files that are part of your application, and a different method to access files that are just out on the machine somewhere.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Access file using servlets, provided file path
 
Similar Threads
Accessing Files from a Web App
Accessing file on remote windows share
Add application context to WAS 5.0
How to write into a file with a servlet (write a remote file)
Accessing data in .txt file from a servlet...