How to get file system path of the context root of any application

风格不统一 提交于 2019-12-20 01:58:30

问题


I am working on web application.I invoke on my jsp request.getContextPath(), but strangely I got address /streetshop.

Then I am appending some path as request.getContextPath() + "abc" and create folder.

Then its creating folder in D:// instead of my webapplication folder.

Please, tell me, I want to upload an image in put it in my web-application root/images/images.gif.


回答1:


You mix things up here. HttpServletRequest.getContextPath() returns your web application root path. In your example this is /streetshop, so your URL may look similar to www.myapp.com/streetshop. If you want to access the internal file system path, you must obtain it from the ServletContext using request.getServletContext().getRealPath("/"). This should return the location of your WAR files' WebContent folder.

Keep in mind that if you modify contents of this path during runtime, you're going to loose everything when redeploying your application.



来源:https://stackoverflow.com/questions/7460608/how-to-get-file-system-path-of-the-context-root-of-any-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!