Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find actual path of webcontent

Im doing a small webb application in JSP which needs to have a file upload function.

Problem is that I cant find out how to get the relative path to my webcontents folder. I need a dynamic way to get there so I can save the files in a folder relative to that path.

So far I have tried:

  String requestURI = request.getRequestURI();
  String contextPath = request.getContextPath();
  String realPath = request.getRealPath();
  String servletPath = request.getServletPath();

None of them gives the actual WebContents path. I guess there is one method I have overlooked but cant find out which.

like image 849
Einar Sundgren Avatar asked Dec 29 '25 17:12

Einar Sundgren


2 Answers

use

request.getServletContext().getRealPath("/");

  • servletContext.getRealPath()
like image 68
jmj Avatar answered Jan 01 '26 06:01

jmj


Don't do that! First, getRealPath works only if your webapp is expanded and second if you redeploy your app, the entire upload is wiped out.

like image 28
Michael-O Avatar answered Jan 01 '26 06:01

Michael-O



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!