RE: how to display image from DB table (blob format)

thumbnail
akash rathod, modified 13 Years ago. New Member Posts: 22 Join Date: 10/6/11 Recent Posts
hi friends
i have store my image in mysql table(blob format) .i am using liferay portal 6.0.and i want to display this image on my portlet how to do it?
plz help.
thumbnail
Hitoshi Ozawa, modified 13 Years ago. Liferay Legend Posts: 7942 Join Date: 3/24/10 Recent Posts
The liferay's way is to create service.xml and portal-model-hints.xml and may be ext-spring.xml as well.
thumbnail
Mahammad Ashik A, modified 6 Years ago. Junior Member Posts: 81 Join Date: 7/17/17 Recent Posts
Hi,
I am facing same issues.

I am storing image as a blob in db. Now i need to disply that blob image in jsp.

How to resolve this.
thumbnail
Olaf Kock, modified 6 Years ago. Liferay Legend Posts: 6441 Join Date: 9/23/08 Recent Posts
Mahammad Ashik A

I am storing image as a blob in db. Now i need to disply that blob image in jsp.
I'm assuming that you know how to get the image out of the database, and which image format it is.

In order to provide a URL for it, I'm assuming that you have a portlet (that contains your JSP). In that portlet, use a <portlet:resourceURL>, parameterize it with (e.g.) the image's id. In the portlet's serveResource method, use that id to find the appropriate image, set the content type and stream the image to the browser (resourceResponse.getOutputStream()).
thumbnail
Mahammad Ashik A, modified 6 Years ago. Junior Member Posts: 81 Join Date: 7/17/17 Recent Posts
Hi  Olaf Kock ,

​​​​​​​Thanks for your support
thumbnail
Mahammad Ashik A, modified 6 Years ago. Junior Member Posts: 81 Join Date: 7/17/17 Recent Posts
Hi,

I resolved this issues by below code.

<%

Blob blob= <fetch from DB>

int myblobLength = (int) blob.length();  
byte[] myblobAsBytes = blob.getBytes(1, myblobLength);
String qrCodeDisplay = DatatypeConverter.printBase64Binary(myblobAsBytes); 
​​​​​​​
%>
  
<div class="container text-center">
    <img  class="img-responsive" src="data:image/jpg;base64, <%=qrCodeDisplay%>"/>                            
  
</div>