shiva
shiva

Reputation: 730

Spring MVC display uploaded image into a External Folder

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?

Upvotes: 1

Views: 4247

Answers (2)

shiva
shiva

Reputation: 730

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.

Upvotes: 6

Rizstien
Rizstien

Reputation: 802

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">

Upvotes: 1

Related Questions