advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Download the code for this article
What kinds of complications have you run into when trying to stream character and binary data with the same servlet?
Partners & Affiliates
advertisement
advertisement
Average Rating: 5/5 | Rate this item | 2 users have rated this item.
 Print Print
 
Stream Both Character and Binary Data via a Servlet
Writing a servlet that can perform character streaming is easy, but streaming binary content is trickier. This simple six-step tutorial guides you through a streaming solution that enables you to stream both character and binary data. 

advertisement
very programmer can quite easily write a servlet that can perform character streaming of XML or HTML data. As long as the proper MIME type is set before the data is streamed, the process is pretty simple (response.setContentType("text/xml");


sets XML content, while response.setContentType("text/html"); sets HTML).

The following code provides an example of streamCharacterData(), the method for character streaming:


/*
* This Method handles streaming Character data
* <p>
* @param String urlstr ex: http://localhost/test.pdf etc.
* @param String format ex: xml or HTML etc.
* @param PrintWriter outstr
* @param HttpServletResponse resp
*/
private void streamCharacterData(String urlstr,String format,
PrintWriter outstr, HttpServletResponse resp) { String ErrorStr = null; try{ //find the right MIME type and set it as contenttype resp.setContentType(getMimeType(format)); InputStream in = null; try{ URL url = new URL(urlstr); URLConnection urlc= url.openConnection(); int length = urlc.getContentLength(); in = urlc.getInputStream(); resp.setContentLength(length); int ch; while ( (ch = in.read()) != -1 ) { outstr.print( (char)ch ); } } catch (Exception e) { e.printStackTrace(); ErrorStr = "Error Streaming the Data"; outstr.print(ErrorStr); } finally { if( in != null ) { in.close(); } if( outstr != null ) { outstr.flush(); outstr.close(); } } } catch(Exception e){ e.printStackTrace(); } }

In much the same way, you can also stream binary content, such as PDF, audio, video, image, doc, and XLS files, but the process is trickier than streaming character data. For example, if you try to stream a PDF file that contains a special image or font with the same servlet you use to stream XML or HTML content, the PDF reader will raise an error: "There was an error processing a page. Too few Operands."

The way to avoid such errors when you're trying to stream both character and binary data is to stream the binary content right way. This article provides a streaming solution that simplifies this procedure. Figure 1 shows an overall view of the solution's design.

Click to enlarge
Figure 1: Streaming Both Character and Binary Data via a Servlet

This simple six-step tutorial guides you through the streaming solution.



How do I stream both binary and character data with a servlet when I can't just use the java.io class PrintWriter?



Write a servlet that sets the appropriate MIME type and utilizes ServletOutPutStream to stream both binary and character data.

  Next Page: Steps 1-3


Page 1: IntroductionPage 3: Steps 4-6
Page 2: Steps 1-3 
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM eBook: Planning a Service Oriented Architecture
IBM eBook: Choosing the Right Architecture--What It Means for You and Your Business
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Avaya Article: Using Intelligent Presence to Create Smarter Business Applications
Intel Go Parallel Article: Getting Started with TBB on Windows
Microsoft Article: 7.0, Microsoft's Lucky Version?
Avaya Article: How to Feed Data into the Avaya Event Processor
IBM Article: Developing a Software Policy for Your Organization
Microsoft Article: Managing Virtual Machines with Microsoft System Center
Intel Go Parallel Article: Intel Threading Tools and OpenMP
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Silverlight 2 App and Walkthrough: Leverage Silverlight 2 with SQL Server and XML
IBM Article: Enterprise Search--Do You Know What's Out There?
HP Demo: StorageWorks EVA4400
Microsoft Article: The Progress and Promise of Deep Zoom
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES