开发者

Get Absolute path of a url in java

开发者 https://www.devze.com 2023-03-02 22:30 出处:网络
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

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.开发者_StackOverflow

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

Thanks in advance.


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.


I don't know much about JAVA.

May be getServletContext().getContextPath() is something you are looking for

EDIT:
Or may be getRealPath()


Tomcat is not a http server. All tomcat urls reference services, not files.

You'll have to implement another service that sends the csv file on request, if you want to get it through any http URL. URL's like http://localhost/myapp/input.csv require a http server like apache httpd.

(Hope I got your question correct...)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号