Accessing files / directories in the webapp folder in the spring

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:

    Test

    
        test
        org.springframework.web.servlet.DispatcherServlet
        1
    

    
        test
        /
    

    
        contextConfigLocation
        /WEB-INF/applicationContext.xml
    

    
        org.springframework.web.context.ContextLoaderListener
    

    
        10
    

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?

How exactly did you deploy your application?

ServletContext().getRealPath("/")
may return null if it is not deployed as exploded. Read the link below for further information. However, the method to configure this may not be the same for your servlet container.

http://ananthkannan.blogspot.com/2009/12/servletcontextgetrealpath-returns-null.html

Updates

Can someone tell me what's the difference in deploying the WAR as exploded?

When you deploy the war file as exploded, the servlet container, e.g. Tomcat, will extract the content of war file into a temporary folder and runs everything from that folder so the {WEB_ROOT]/_images/avatars/[myid].jpg
is actually exist on file system (hard disk). Therefore, you can actually get the real path (as it already says in the name of the method). However, if your servlet container does not extract the war file, the folder you are looking for is inside the war file and there is no real path to it so it will return null
.

Is it something not recommended to do on a production server? Advantages/disadvantages?

You should not store dynamic contents under your source folder or the webroot folder (webapp) since servlet container will use it temporarily and delete it or change to a new folder when you redeploy your web application. You will likely lost the dynamic data you put into these folders. The web root folder is usually designed for storing static content, which means content you don’t want to change – for example, graphic images for your web component like background images, css, etc.

The usual method for storing user data is to somehow create a folder in userspace and put your dynamic data in there. However, you will not be able to serve the content of the folder outside webroot. You will need to write your own static servlet to pipe the data when they are requested. This is quite complicated for a beginner.

The easiest way for implementing your own static servlet to serve dynamic content is to extend the static servlet of your servlet container. However, your code will highly depend on the servlet container you are deploying to.

Since you are going to provide a REST interface for resizing images, you can create a controller which reads in the original images from the dynamic content folder, do the resizing, save it as a temporary file or flush the content of the HttpResponse
.

Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » Accessing files / directories in the webapp folder in the spring

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录