Gnik
Gnik

Reputation: 7426

How to read a directory in webapp folder of Maven application

I'm working in Maven web application (Java). I need to read a directory (for ex: mydirectory) in my webapp folder as follows:

File file = new File("path of my directory");

I just specify the hierarchy of folders in my application.

MyApplication->src->main->webapp->MyDirectory

But I'm going to write my Java code I the package as follows,

MyApplication->src->main->java->com->mypackage->myfile.java

From myfile.java I need to read the directory "MyDirectory" in webapp. As new File("path of the directory")

But I don't know how to specify the path of the directory here.

Upvotes: 2

Views: 5767

Answers (4)

Matthias
Matthias

Reputation: 400

I couldn't access the servlet context since the service where i need to read data from the webapp folder is not aware of the current servlet context.

Anyone who is using Spring can never the less access the webapp folder by simply implementing the ResourceLoaderAware Interface provided by the spring framework.

public class SomeService implements ResourceLoaderAware {    
    
    private ResourceLoader resourceLoader;
    
    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }
    
    public readFromWebappFolder() {
        Resource resource = resourceLoader.getResource("path/to/file/in/webappFolder");
    }
}

Upvotes: 0

t0r0X
t0r0X

Reputation: 4782

You haven't mentioned what technology/framework you use, but I suppose you're using at least Java servlets, because you mentioned it's a web application. So you can use ServletContext.getRealPath() to get the path in the filesystem:

String fullPath = getServletContext().getRealPath("/MyDirectory");
System.out.printf("real filesystem path: %s", fullPath);

Update:

Please note that JavaDoc of the method says:

The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators.

Resources inside the /META-INF/resources directories of JAR files bundled in the application's /WEB-INF/lib directory must be considered only if the container has unpacked them from their containing JAR file, in which case the path to the unpacked location must be returned.

This method returns null if the servlet container is unable to translate the given virtual path to a real path.

This means that the returned path:

  1. is not portable (e.g. in Windows with \, in Linux/macOS with /, etc).
  2. might be null, e.g. if the resource is virtually in memory, in a JAR file, or so.

Therefore: please follow my answer only with those caveats in mind, and better follow the other answers using ServletContext.getResourceAsStream()

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691903

At runtime, there is no maven anymore (thank God!), and there is no directory anymore. All you have is a war file, and everything is either in the war file, or somewhere outside of the app, on the file system.

Use ServletContext.getResourceAsStream() to load a file from the webapp's context.

Upvotes: 1

Emil L
Emil L

Reputation: 21111

Try

ServletContext context = getServletContext();
InputStream is = context.getResourceAsStream("/MyDirectory/FileInThatDir.txt");

alternatively use getResource() instead of getResourceAsStream()

Upvotes: 2

Related Questions