Java – how to get the file system path of the context root of any application

I'm working on Web applications I'm in my JSP request Called on getcontextpath (), but strangely, I got the address / Street store

Then I append some paths as request Getcontextpath() "ABC" and create a folder

Then create a folder in D: / / instead of my web application folder

Please tell me that I want to upload pictures to my web application root / images / images Gif

Solution

You mixed something here HttpServletRequest. Getcontextpath() returns the root path of your web application In your example, this is / streetshop, so your web address might be similar to www.myapp.com com/streetshop. If you want to access the internal file system path, you must use request getServletContext(). Getrealpath ("/") gets it from ServletContext This should return the location of the webcontent folder of the war file

Remember that if you modify the contents of this path at run time, you will lose all content when you redeploy the application

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>