mjs
mjs

Reputation: 22347

Spring MVC Get file under WEB-INF without a request

I am trying to get hold of a file ( or a directory ) under /WEB-INF/.../

outside of a request. I need it in a bean loaded at server startup.

All solutions I can find either wants an XML file using ClassPathXmlApplicationContext or a request to get the servlet context or using the current executing class. Seems ugly to me.

How can I get a File("/WEB-INF/myDir/"). There has to be a way, no!?

Upvotes: 27

Views: 44499

Answers (7)

gene b.
gene b.

Reputation: 11974

request.getSession().getServletContext().getResourceAsStream("yourfile.pdf");

Upvotes: -1

Kirill
Kirill

Reputation: 8046

Not completely related to your question, but... Here is some universal sulution i used to load properties from anywhere in web application like Spring does it (supporting WEB-INF/..., classpath:..., file:...). Is is based on using ServletContextResourcePatternResolver. You will need ServletContext.

private static Properties loadPropsTheSpringWay(ServletContext ctx, String propsPath) throws IOException {
    PropertiesFactoryBean springProps = new PropertiesFactoryBean();
    ResourcePatternResolver resolver = new ServletContextResourcePatternResolver(ctx);
    springProps.setLocation(resolver.getResource(propsPath));
    springProps.afterPropertiesSet();
    return springProps.getObject();
}

I used the above method in my custom servlet context listener while conext was not yet loaded.

Upvotes: -1

mahesh nanayakkara
mahesh nanayakkara

Reputation: 616

I use Spring DefaultResourceLoader and Resource to read inside WEB-INF or any resources in a *.jar file. Work like a charm. Good luck!

import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;

public static void myFunction() throws IOException {
    final DefaultResourceLoader loader = new DefaultResourceLoader();               
    LOGGER.info(loader.getResource("classpath:META-INF/resources/img/copyright.png").exists());             
    Resource resource = loader.getResource("classpath:META-INF/resources/img/copyright.png");           
    BufferedImage watermarkImage = ImageIO.read(resource.getFile());
}

Upvotes: 11

Murali Krish
Murali Krish

Reputation: 65

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("files/test.xml").getFile());

"files" folder should be child of "main/resources" folder

Upvotes: 3

ACV
ACV

Reputation: 10562

This is how you can do it if you just want to access it from a Service (not through ServletContext):

    final DefaultResourceLoader loader = new DefaultResourceLoader();
    Resource resource = loader.getResource("classpath:templates/mail/sample.png");
    File myFile = resource.getFile();

Note that the last line may throw IOException so you need to catch / rethrow

Note, that the file is here: src\main\resources\templates\mail\sample.png

Upvotes: 0

axtavt
axtavt

Reputation: 242686

As long as your bean is declared in web application context you can obtain an instance of ServletContext (using ServletContextAware, or by autowiring).

Then you can access files in webapp directory either directly (getResourceAsStream(), getRealPath()), or using ServletContextResource.

EDIT by momo:

@Autowired
ServletContext servletContext;

... myMethod() { 
     File rootDir = new File( servletContext.getRealPath("/WEB-INF/myDIR/") );
}

Upvotes: 49

NimChimpsky
NimChimpsky

Reputation: 47280

You can use classpath resource if the file is located in the WEB_INF\classes directory. Which is where any files in your src/main/resources directory will be copied to using a normal maven build ...

import org.springframework.core.io.Resource
...
final Resource yourfile = new ClassPathResource( "myfile.txt");

Upvotes: 2

Related Questions