11

I need to get real path for file in my WebContent directory, so that framework that I use can access that file. It only takes String file as attribute, so I need to get the real path to this file in WebContent directory.

I use Spring Framework, so solution should be possible to make in Spring.

newbie
  • 24,286
  • 80
  • 201
  • 301

9 Answers9

13

getServletContext().getRealPath("") - This way will not work if content is being made available from a .war archive. getServletContext() will be null.

In this case we can use another way to get real path. This is example of getting a path to a properties file C:/Program Files/Tomcat 6/webapps/myapp/WEB-INF/classes/somefile.properties:

// URL returned "/C:/Program%20Files/Tomcat%206.0/webapps/myapp/WEB-INF/classes/"
URL r = this.getClass().getResource("/");

// path decoded "/C:/Program Files/Tomcat 6.0/webapps/myapp/WEB-INF/classes/"
String decoded = URLDecoder.decode(r.getFile(), "UTF-8");

if (decoded.startsWith("/")) {
    // path "C:/Program Files/Tomcat 6.0/webapps/myapp/WEB-INF/classes/"
    decoded = decoded.replaceFirst("/", "");
}
File f = new File(decoded, "somefile.properties");
A Kunin
  • 42,385
  • 1
  • 17
  • 13
13

If you need this in a servlet then use getServletContext().getRealPath("/filepathInContext")!

Shyam Kumar Sundarakumar
  • 5,649
  • 13
  • 42
  • 69
2

you must tell java to change the path from your pc into your java project so if you use spring use :

@Autowired
ServletContext c;

String UPLOAD_FOLDEdR=c.getRealPath("/images"); 

but if you use servlets just use

String UPLOAD_FOLDEdR = ServletContext.getRealPath("/images");  

so the path will be /webapp/images/ :)

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
Raslan
  • 11
  • 1
0

This approach uses the resource loader to get the absolute path to a file in your app, and then goes up a few folders to the app's root folder. No servlet context required! This should work if you have a "web.xml" in your WEB-INF folder. Note that you may want to consider using this solely for development, as this type of configuration is usually best stored externally from the app.

public String getAppPath()
{
    java.net.URL r = this.getClass().getClassLoader().getResource("web.xml");
    String filePath = r.getFile();
    String result = new File(new File(new File(filePath).getParent()).getParent()).getParent();

    if (! filePath.contains("WEB-INF"))
    {
        // Assume we need to add the "WebContent" folder if using Jetty.
        result = FilenameUtils.concat(result, "WebContent");
    }

    return result;
}
Brad Parks
  • 66,836
  • 64
  • 257
  • 336
0

In situations like these I tend to extract the content I need as a resource (MyClass.getClass().getResourceAsStream()), write it as a file to a temporary location and use this file for the other call.

This way I don't have to bother with content that is only contained in jars or is located somewhere depending on the web container I'm currently using.

tangens
  • 39,095
  • 19
  • 120
  • 139
  • @tangens - I don't think he wants to read a resource from the webapps classloader; e.g. a file in a JAR in a WAR. He just wants to get the pathname of a file within the web container. – Stephen C May 07 '10 at 06:05
0

Include the request as a parameter. Spring will then pass the request object when it calls the mapped method

@RequestMapping .....
public String myMethod(HttpServletRequest request) {
   String realPath = request.getRealPath("/somefile.txt");
   ...
objects
  • 8,637
  • 4
  • 30
  • 38
0

You could use the Spring Resource interface (and especially the ServletContextResource): http://static.springsource.org/spring/docs/current/javadoc-api/org/springframework/core/io/Resource.html

Thomas Vanstals
  • 174
  • 1
  • 2
0

my solve for: ..../webapps/mydir/ (..../webapps/ROOT/../mydir/)

String dir = request.getSession().getServletContext().getRealPath("/")+"/../mydir";
            Files.createDirectories(Paths.get(dir));
erc mgddm
  • 1
  • 3
-1

try to use this when you want to use arff.txt in your development and production level too

  String path=getServletContext().getRealPath("/WEB-INF/files/arff.txt");
Shaaban Ebrahim
  • 9,766
  • 1
  • 18
  • 20
  • While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – yivi Dec 29 '17 at 10:05