Spring MVC 未解析存储在 webroot 外部的图像路径

Spring MVC not resolving image path stored outside webroot

我正在上传图片和视频,并为视频和音频创建相应的标签以显示在视图页面上,但不知何故图片和视频路径无法解析。

这是我的控制器。

@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;
}    

我试过调度程序中的资源设置。

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

上传的图片路径。

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

请向我建议所需的更改。

'///' 三重斜杠解决了我的问题。现在我的资源映射是这样的

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

遇到此问题的任何人请阅读 JB Nizet 的评论并阅读此 link How to retrieve and display images from a database in a JSP page?

使用下面的代码行,它可能会对你有所帮助。

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