1

I am creating a image uploading app using Spring mvc . I saved the image successfully in a external folder. Code for saving the image is like this:

  MultipartFile productImage = product.getProductImage();

        path= Paths.get("/oms/images/"+product.getProductName()+".jpg");

        System.out.println(path.toString());
        if(productImage!=null && !productImage.isEmpty()){

            try {
                productImage.transferTo(new File(path.toString()));
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("Product Image Saving Failed ",e);

            }
        }

Image is getting saved into that external folder successfully. But when I am trying to display the same image into a jsp file, I end up getting this error....

http://localhost:8081/oms/images/Ss.jpg Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8081/oms/images/pup.jpg Failed to load resource: the server responded with a status of 404 (Not Found) 

the code to display the image is like this...

  <img src="<c:url value="/oms/images/${product.productName}.jpg"/>"/>

I saw some more couple of links for the same issue, but mostly question are like to save images into a different directory than webapp (obviously)!

Am I missing something? How to add external resources into Spring? What should I do to display user added uploaded images into jsp page?

0

2 Answers 2

6

The above solution didn't work for me. If I tried calling @WebServlet("/FetchFile") from the jsp page, @WebServlet("/FetchFile") didn't even get invoked . I Googled it and found a solution that in spring mvc dispatcher-servlet.xml we can add a external resource in this way:

<mvc:resources mapping="/images/**" location="file:///${fullPath}/oms/images/" />

${fullPath} you should give your full path here:

then from jsp page you can invoke the jsp file like this:

<img src="/images/${product.productName}.jpg" alt="image"/>

After full day I finally got the solution and it worked for me.

1

You can use a servlet to retrieve images outside webcontext as jsp code will only work within web context. As an example

@WebServlet("/FetchFile")
public class FileServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        String filename = request.getParameter("productName");
        File file = new File("/oms/images/"+filename);
        response.setHeader("Content-Type", getServletContext().getMimeType(filename));
        response.setHeader("Content-Length", String.valueOf(file.length()));
        response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
        Files.copy(file.toPath(), response.getOutputStream());
    }

}

and request it using

<img scr="FetchFile?productName=${product.productName}.jpg">
6
  • you mean I should add this method to spring controller and then from jsp page i should make a call to this method ?
    – shiva
    Jun 16, 2016 at 10:08
  • Yes ! That would be an ajax call by nature
    – Rizstien
    Jun 16, 2016 at 10:09
  • I did ... i copied the class file and I am trying to access it by .. <td> <img scr="FetchFile?productName=${product.productName}.jpg" alt="image"/> </td> .... but not working for me ... I put syso also in that class file looks like that file is not getting invoked also ...what's wrong ? I am using tomcat 7 and tomcat 8 also ... same result .. what can be the issue @WebServlet("/FetchFile") is not getting invoked . I am not getting any error in browser console. Can you please check this ?
    – shiva
    Jun 16, 2016 at 11:36
  • 1
    Thanks @Rizstien .. finally this worked for me ... <mvc:resources mapping="/images/**" location="file:///${fullPath}/oms/images/" />
    – shiva
    Jun 16, 2016 at 14:06
  • sorry there was some problem may be i couldn't view your complete comment. The problem was the servlet mapping technique, may be that was not working with your configurations. the idea was to make a servlet and download resource from it
    – Rizstien
    Jun 17, 2016 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.