Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Absolute path of a url in java

Tags:

java

tomcat

Is there a way to retrieve the absolute path of url (http://localhost:8080/myApp) in java. The scenario is, i need to connect to csv file located in tomcat server. The statement works well if I enter the absolute path, but is there a solution to retrieve url's path using getAbsolutePath().Sorry if I'm wrong.

Connection conn = DriverManager(getConnection("jdbc:relique:csv:/home/apache-tomcat-6.0.26/webapps/myApp/"))

Thanks in advance.

like image 262
Ria Avatar asked May 04 '11 09:05

Ria


People also ask

How do I find the absolute path of a URL?

To get absolute path using relative/shorten url: add shorten url & click on resolve. https://www.test.com/en/testPage -> /content/mywebsite/en/testPage //make sure you are passing complete path with domain name as per your etc mapping.

What is absolute path in Java?

An absolute path always contains the root element and the complete directory list required to locate the file. For example, /home/sally/statusReport is an absolute path. All of the information needed to locate the file is contained in the path string.

How do you create an absolute path in Java?

The getAbsolutePath() method is a part of File class. This function returns the absolute pathname of the given file object. If the pathname of the file object is absolute then it simply returns the path of the current file object. For Example: if we create a file object using the path as “program.


1 Answers

You can use ServletContext.getRealPath(), which does exactly what you want.

Note that it does not necessarily work in all situations. For example, if your Tomcat is configured to deploy the .war file without unpacking it, then this will return null.

like image 166
Joachim Sauer Avatar answered Sep 19 '22 00:09

Joachim Sauer