I need to know the Java equivalent of PHP's $_SERVER['DOCUMENT_ROOT']
.
I am writing a Java backend program that will take an uploaded image as a byte[] and save the image to the server where my Java program is running. Then I want to send the image url to the user (front-end) or as email; so that the user can click on the url I send to view the image.
In PHP, I use $_SERVER['DOCUMENT_ROOT']
plus the relative path of the file. How do I do that in Java?
I am not using servet. I am using Jersey to return the url. (If that's impo开发者_运维百科rtant)
I need the returned path to be "http://localhost:8080/mypics/pic1.jpg" when the server is localhost and "http://www.mysite.com/mypics/pic1.jpg" when running on a live server. The new File("./") code is not returning "http..."; netbeans cannot find the method ServletContext.getContextPath(), after importing
import javax.servlet.ServletContext;
You can use ServletContext.getContextPath()
to get context path or use ServletContext.getRealPath(String path)
to get real path for a given virtual path.
The simplest way if you not use servlet API is to create File object point to the ./:
System.out.println(new File("./").getAbsolutePath());
In this case you should see in console path to your application directory. This could be your $_SERVER['DOCUMENT_ROOT']
for this application.
精彩评论