advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : Using FOP With Java - Graphics with FOP :

Loading Images From Non-file resources

As we have seen that loading images from external file resources is fairly easy with XSL-FO. However, recently, I faced a situation where I had to load and display images with FOP when images are stored in a database using Binary Large Objects (BLOB). In this situation, I am not allowed to store the image in any file system. This is a tricky situation and often daunting with limiting prospects. However, there is a clever way to still be able to use these sorts of images. I figured out the following solution architecture for loading and displaying BLOB images using XSL-FO and FOP produce a FOP document.


Figure-1 The BLOB image loading architecture

In this architecture, I used a Data Access Object (DAO) to get the images as a BLOB from the database. A Servlet uses this DAO to get the image data in binary form from the database. The XSL-FO in turn provides the URL of this Servlet as the image source. By this mechanism, the Servlet will serve the image as a binary data stream and XSL –FO will interpret the binary stream as image data and render it as an image.

However straightforward this solution seems, there are few salient points to take note of.

  • How do we tell the Servlet, which image to bring back? This means we need a mechanism to pass on to the Servlet some sort of unique identifier for the image. If the image is stored in a database as a BLOB, we can assign a unique id as primary key and pass this primary key value to the Servlet and in turn the DAO to bring back the correct image.
  • In order the Servlet to serve the request from XSL-FO, the Servlet must support GET protocol to obtain the data.
  • The Servlet when sending the response back must set the content type as part of the response. This helps the XSL-FO to understand the type of image it is dealing with.

Below is an example code for a Servlet to send the image binary stream back to the caller.

public class MediaServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse 
res) throws ServletException, IOException {
                OutputStream stream = null;
                Attachment attachment = null;
 
                                String attUrn = 
req.getParameter("attachmentURN");
            AttachmentService attachmnetService = 
AttachmentServiceFactory.getInstance();
                           try {
                                 stream = res.getOutputStream();
                  attachment = attachmentService.getAttachment(attUrn);
 
        if(attchment !=null){
             res.setContentType("image/gif");
                res.setContentLength(attachment.getLength());
                    stream.write(attachment.getAttachmentByte());
                  }
             }catch(Exception e){
             }finally{
                    stream.flush();
                    stream.close();
             }
                }
 
}

Listing 1: The MediaServlet program

In this example, we obtain the image as a binary stream using the AttachmentService Data Access Component. We pass in the attachment URN which is the primary key for the image in the database, and the service returns an Attachment object that contains the binary data for the image. We then write the binary stream back to the caller outputStream.

If we make a call to this service form a XSL-FO stylesheet the MediaServlet will stream the image binary stream back to the XSL-FO stylesheet. XSL-FO will in turn interpret the binary stream by looking at its content-type and content-length and display the image.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs