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;
    try
    {
        String path = request.getServletContext().getRealPath("/");
        File directory = null;
        if (multipartFile.getContentType().contains("image"))
        {
            directory = new File(path + "/uploads/images/");
            isImage = true;
        }
        else
        {
            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));
        stream.write(bytes);
        stream.close();

        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.

/home/govi/demo/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/projectImg/uploads/images/batman.jpg

      

suggest me the changes you need.

+3


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?

0


source


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



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

      

+1


source







All Articles