Spring MVC does not resolve image path stored outside webroot

I load images and videos and create appropriate video and audio tags to display on the view page, but somehow the image and video path is not getting resolved.

Here is my controller.

@RequestMapping(value = "/contentUpload", headers = "content-type=multipart/*", method = RequestMethod.POST)
public @ResponseBody
String uploadImage(@RequestParam("fileData") MultipartFile multipartFile, HttpServletRequest request )
    String jsonResponse = null;
    boolean isImage = false;
    boolean isVideo = false;
        String path = request.getServletContext().getRealPath("/");
        File directory = null;
        if (multipartFile.getContentType().contains("image"))
            directory = new File(path + "/uploads/images/");
            isImage = true;
            directory = new File(path + "/uploads/videos/");
            isVideo = true;

        byte[] bytes = null;
        File file = null;
        bytes = multipartFile.getBytes();

        if (!directory.exists()) directory.mkdirs();

        file = new File(directory.getAbsolutePath() + System.getProperty("file.separator") + multipartFile.getOriginalFilename());
        BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(file));

        if (isImage)
            jsonResponse = "<br /><img src=\"" + file.getAbsolutePath() + "\" />";
        else if (isVideo)
            jsonResponse = "<video>" + "<source src=\"" + file.getAbsolutePath() + "\" type=\"" + multipartFile.getContentType() + "\">" + "</video>";
    catch (Exception e)
    return jsonResponse;


I tried resource settings in the manager.

<mvc:resources mapping="/uploads/**" location="/#{servletContext.contextPath}/uploads/" />


loaded image path.



suggest me the changes you need.


source to share

2 answers

The '///' triple slash helped solve my problem. Now my resource mapping looks like this.

<mvc:resources mapping="/uploads/**" location="file:///home/govi/uploads/" />


Anyone who ran into this issue, check out JB Nizet's comments and also check out this link How to get and display images from a database in a JSP page?



Use the line of code below, it may help you.

<mvc:resources mapping="/uploads/**"  location="/WEB-INF/projectImg/uploads/" />




All Articles