This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes View stored blob image in the UI using java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "View stored blob image in the UI using java" Watch "View stored blob image in the UI using java" New topic
Author

View stored blob image in the UI using java

ruba sampath
Greenhorn

Joined: Jun 17, 2011
Posts: 28
Hi,

I am storing an image or pdf in the database as blob type.

Now i need to disply the image in the UI.

How to disply the image in the UI screen using java code?

Kindly guide us to proceed further.

Thank you.




Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

For images it's quite easy. Use ImageIO to convert the binary data into a BufferedImage, then use ImageIcon and JLabel for displaying it.
For PDF files it's harder, but I believe PDF renderer can help you out.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
ruba sampath
Greenhorn

Joined: Jun 17, 2011
Posts: 28
Thank you. But i am not using swing or applet in my application. Is it possible to display through ServletOutputStream?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Sure. You need a separate servlet that will a) set the right content type, b) if possible, set the content size, and c) include only the image / PDF file data and no other data (like leading / trailing whitespace). The latter makes JSP pages not very good for binary data like images or PDF files, as it's really easy to accidentally add leading whitespace. If you decide to use a JSP page, make sure that there are no line breaks whatsoever outside of the JSP tags - not even between directives.
ruba sampath
Greenhorn

Joined: Jun 17, 2011
Posts: 28
Thank you very much. Its working now. But how to restrict only upload images and pdf and not other types.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

I believe that in HTML5 you can specify file extensions to filter on, but in HTML4 and before you can only refuse to process the files in the form's action page (servlet / JSP / PHP / ...).
 
GeeCON Prague 2014
 
subject: View stored blob image in the UI using java