shantan kumar
shantan kumar

Reputation: 147

How to display a image in jsp, which is located in root directory of server

I am new to spring MVC and I have a application which store images in the root directory of the application.

The directory is something this like this : C:\Users\Golla\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\medicalus\images\1423310234176.png

My Project's name is medicalus and the images folder is used to save images. Now I want to retrieve it and display it in the jsp page. I have tried giving up the complete path of images and tried adding in < mvc:resources> tag of servlet dispatcher but none of these worked.

How can I display images which are located in the root directory of the server? Any suggestion would be appreciated.

Upvotes: 1

Views: 1866

Answers (2)

Md. Samim hossain
Md. Samim hossain

Reputation: 265

Suppose you store image in external folder of the spring project. And location of the image you store in the database. On that case you can send the location of the image to jsp page from controller in base64 format. To convert in base64 format you can follow the following code:

String covertPathTo64Base(String path) throws Exception{

    InputStream is = new FileInputStream(path);
    byte[] bytes = new byte[is.available()];
    is.read(bytes);

    Base64.Encoder encoder = Base64.getEncoder();
    String imagePath = encoder.encodeToString(bytes);

    return imagePath;
}

Upvotes: 0

Paul Vargas
Paul Vargas

Reputation: 42060

Try this:

  1. In the Spring's configuration file:

    <mvc:resources mapping="/images/**" location="/images/" />
    
  2. In you JSP:

    <img src="${pageContext.request.contextPath}/images/1423310234176.png" />
    

Upvotes: 2

Related Questions