Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing files/directories in webapp folder in Spring
    primarykey
    data
    text
    <p>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:</p> <pre><code>-src --main ---java (marked as source root) ----my -----package ------controller -------Home.java ---webapp ----images -----avatars </code></pre> <p>My code in Home.java:</p> <pre><code>@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"; } } </code></pre> <p><s>All the 5 requests return null. Am I doing something wrong?</s></p> <p>web.xml:</p> <pre><code>&lt;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"&gt; &lt;display-name&gt;Test&lt;/display-name&gt; &lt;servlet&gt; &lt;servlet-name&gt;test&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;test&lt;/servlet-name&gt; &lt;url-pattern&gt;/&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;session-config&gt; &lt;session-timeout&gt;10&lt;/session-timeout&gt; &lt;/session-config&gt; &lt;/web-app&gt; </code></pre> <p>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.</p> <p>Thank you!</p> <p><strong>Update:</strong> simplified the example for better understanding.</p> <p><strong>Update2:</strong> 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?</p> <p>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 <code>src/main/webapp/_images/avatars/[myid].jpg</code> folder. Is it recommended to upload pictures to the <code>webapp</code> 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 <code>/images/[width]x[height]/[userid].jpg</code>.</p> <p>Deploying the WAR as exploded and implementing the <code>ResourceLoaderAware</code> (thanks @KevinSchmidt), I can make it work using this:</p> <pre><code>resourceLoader.getResource("file:" + request.getSession().getServletContext().getRealPath("/") + "_images/avatars/"); </code></pre> <p>To me it looks quite dirty, is it a good idea for a production server? Is there a better solution?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload