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

Access file using servlets, provided file path

 
kavin clain
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to access file saved in server using servlets. Actually the problem is how to specify file path?
 
Sharad Jadhav
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String path = getServletContext () .getRealPath ("xyz.xml" ) ;
System.out.println ( path ) ;
File file = new File ( path ) ;

Hope this will help you
Cheers!!!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just use the file path.
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic