I'm inexperienced in Spring and everything I need to do now is to access and obtain a reference to files and folders in the webapp folder. Here is my relevant project hierarchy:
-src
--main
---java (marked as source root)
----my
-----package
------controller
-------Home.java
---webapp
----images
-----avatars
My code in Home.java:
@Controller
@RequestMapping("/")
public class Home
{
@RequestMapping(method = RequestMethod.GET)
public String index(Model model,
HttpServletRequest request) throws Exception
{
String test1 = request.getSession().getServletContext().getRealPath("");
String test2 = request.getSession().getServletContext().getRealPath("/");
String test3 = request.getRealPath("");
String test4 = request.getRealPath("/");
String test5 = request.getSession().getServletContext().getRealPath(request.getServletPath());
return "index";
}
}
All the 5 requests return null. Am I doing something wrong?
web.xml:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Test</display-name>
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<session-config>
<session-timeout>10</session-timeout>
</session-config>
</web-app>
What I'm trying to achieve (not shown in this example), is to code a controller responsible for the scaling of an image. For this reason I would need access to the src/main/webapp/_images folder.
Thank you!
Update: simplified the example for better understanding.
Update2: thanks to the suggestion of @gigadot, I deployed the application as an exploded WAR and the problem is partly solved. Can someone tell me what's the difference in deploying the WAR as exploded? Is it something not recommended to do on a production server? Advantages/disadvantages?
I think it's worth to explain the situation with an example. Let's say I'm coding a social network and I have to possibility to upload my personal profile picture. This picture will be uploaded to the src/main/webapp/_images/avatars/[myid].jpg
folder.
Is it recommended to upload pictures to the webapp
folder? Or is there a better solution?
I would like to be able to return a scaled instance of the image when accessing the URL /images/[width]x[height]/[userid].jpg
.
Deploying the WAR as exploded and implementing the ResourceLoaderAware
(thanks @KevinSchmidt), I can make it work using this:
resourceLoader.getResource("file:" + request.getSession().getServletContext().getRealPath("/") + "_images/avatars/");
To me it looks quite dirty, is it a good idea for a production server? Is there a better solution?