How to fetch actual web resource source file path in java?

584 views Asked by At

I have developed java application which is actually stored at d drive.I have imported that application into eclipse which is stored at C drive. In my application there is one folder named as 'Image'. So whenever I search for the real Path of image folder through Servlet-context or request etc it gives me the path of C:\eclipse\spring-insight-instance\wtpwebapps\EAPortal\images

But i want to fetch the actual path of that image folder.Means path of images at D drive. How i should fetch it?

Thanks in Advance, Tushar Dodia

1

There are 1 answers

0
Konstantin Komissarchik On BEST ANSWER

I don't believe there is a way to do this. Your web app container knows nothing about Eclipse and how it publishes web apps. In either case, you should not try to do this. Using getRealPath() method is most often the wrong way to approach a problem. In most cases, by the time the app is deployed in production, there is no real path as the app is contained by a .war file.

  1. If the images are static resources, you should be using ServletContext.getResourceAsStream method to read these images.

  2. If the images folder is a location where your app both reads and writes, then it should not be contained by your app. Separate it out from your application logic and then provide the location of the data directory to your app by using Java system property or an environment variable. Your specific web app container may have other options for passing parameters to a web app.