1

I am uploading images and videos and creating respective tags for video and audio to display on view page but somehow image and video path are 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 have tried resources settings in dispatcher.

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

uploaded image path.

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

please suggest me the changes required.

Govinda Sakhare
  • 5,009
  • 6
  • 33
  • 74
  • 2
    Don't, ever, try to store anything under the webapp directory. This directory will, BTW, not exist at all if you deploy a war file. Uploaded images are data. They're not part of the application. They belong to the database or to a data directory outside of the application. You don't want all those data to be lost forever as soon as you redeploy the application. – JB Nizet May 12 '15 at 06:19
  • so where should I store the content? please provide me some example if possible. I am new to web development. – Govinda Sakhare May 12 '15 at 06:22
  • Create an empty, dedicated, directory for the images on your file system, and store the images there. `/home/govi/uploaded-images` for example. – JB Nizet May 12 '15 at 06:25
  • Just like JB Nizet suggested, depending on your scenarios, if many videos and images rather than saving on a data folder and have it saved on the server, I would consider using a Storage Service and save there the data and access it from your web, many benefits to this last approach. –  May 12 '15 at 06:29
  • Read http://stackoverflow.com/questions/2340406/how-to-retrieve-and-display-images-from-a-database-in-a-jsp-page – JB Nizet May 12 '15 at 06:34
  • so, File directory = new File("/home/govi/uploads/images/"); right? – Govinda Sakhare May 12 '15 at 06:54
  • its not working 404 resource not found. – Govinda Sakhare May 12 '15 at 10:43
  • Yes, because you haven't read the answer I linked to, and thus haven't created the servlet needed to display images. – JB Nizet May 12 '15 at 15:51

2 Answers2

1

Use below code line, it may helps you.

<mvc:resources mapping="/uploads/**"  location="/WEB-INF/projectImg/uploads/" />
Suman Behara
  • 150
  • 9
0

'///' triple slash solved my issue. Now my resource mapping is like this.

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

Anyone facing this issue please go through JB Nizet's comments and also go through this link How to retrieve and display images from a database in a JSP page?

Community
  • 1
  • 1
Govinda Sakhare
  • 5,009
  • 6
  • 33
  • 74