Stream Both Character and Binary Data via a Servlet

Stream Both Character and Binary Data via a Servlet

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* 

* @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 = != -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 class PrintWriter?

Write a servlet that sets the appropriate MIME type and utilizes ServletOutPutStream to stream both binary and character data.Step One
Set the appropriate MIME type based on the format with which you’re working. The getMimeType() method supports various MIME types:

/* * This Method Returns the right MIME type for a particular  format * 

* @param String format ex: xml or HTML etc. * @return String MIMEtype */ private String getMimeType(String format) { if(format.equalsIgnoreCase("pdf")) //check the out type return "application/pdf"; else if(format.equalsIgnoreCase("audio_basic")) return "audio/basic"; else if(format.equalsIgnoreCase("audio_wav")) return "audio/wav"; else if(format.equalsIgnoreCase("image_gif")) return "image/gif"; else if(format.equalsIgnoreCase("image_jpeg")) return "image/jpeg"; else if(format.equalsIgnoreCase("image_bmp")) return "image/bmp"; else if(format.equalsIgnoreCase("image_x-png")) return "image/x-png"; else if(format.equalsIgnoreCase("msdownload")) return "application/x-msdownload"; else if(format.equalsIgnoreCase("video_avi")) return "video/avi"; else if(format.equalsIgnoreCase("video_mpeg")) return "video/mpeg"; else if(format.equalsIgnoreCase("html")) return "text/html"; else if(format.equalsIgnoreCase("xml")) return "text/xml"; else return null; }

Step Two
Get the reference to the right OutPutStream. Use ServletOutPutStream, where as for character data you’d use PrintWriter, the class that prints objects to a text-output stream. This following snippet is for binary data:

ServletOutputStream sOutStream = response.getOutputStream();

Step Three
Create BufferedInputStream from the InputStream:

BufferedInputStream bis = null;InputStream in = urlc.getInputStream();bis = new BufferedInputStream(in);

Step Four
Create BufferedOutPutStream with a new ServletOutPutStream to which you can write:

BufferedOutputStream bos = null;              bos = new BufferedOutputStream(sOutStream);

Step Five
Read in to the bytes array from BufferedInputStream:

byte[] buff = new byte[length];int bytesRead;// Simple read/write loop.while(-1 != (bytesRead =, 0, buff.length))) { 	bos.write(buff, 0, bytesRead);}

Step Six
Write on to BufferedOutPutStream from the bytes array, which in turn streams to the client. Use the streamBinaryData() method for binary data streaming:

/* * This Method Handles streaming Binary data * 

* @param String urlstr ex: http://localhost/test.pdf etc. * @param String format ex: pdf or audio_wav or msdocuments etc. * @param ServletOutputStream outstr * @param HttpServletResponse resp */ private void streamBinaryData(String urlstr,String format,
ServletOutputStream outstr, HttpServletResponse resp) { String ErrorStr = null; try{ //find the right MIME type and set it as contenttype resp.setContentType(getMimeType(format)); BufferedInputStream bis = null; BufferedOutputStream bos = null; try{ URL url = new URL(urlstr); URLConnection urlc= url.openConnection(); int length = urlc.getContentLength(); resp.setContentLength(length); // Use Buffered Stream for reading/writing. InputStream in = urlc.getInputStream(); bis = new BufferedInputStream(in); bos = new BufferedOutputStream(outstr); byte[] buff = new byte[length]; int bytesRead; // Simple read/write loop. while(-1 != (bytesRead =, 0, buff.length))) { bos.write(buff, 0, bytesRead); } } catch (Exception e) { e.printStackTrace(); ErrorStr = "Error Streaming the Data"; outstr.print(ErrorStr); } finally { if( bis != null ) { bis.close(); } if( bos != null ) { bos.close(); } if( outstr != null ) { outstr.flush(); outstr.close(); } } } catch(Exception e){ e.printStackTrace(); } }

Installation Procedure
Compile the file provided in this article, and copy the class file to the “doc” directory of your Web server. Be sure to configure your web.xml file to contain information.

Copy a few sample files (.HTML, .pdf, .xml, .gif, .jpeg, .wav, .avi, .mpeg, etc.) into the “doc” directory of your Web server. If you’re using Microsoft IIS, copy them into the wwwroot directory.

URL Request Examples
Try the following URLs to test your streams. Replace “server” with the actual machine name:

  • http://server/servlet/StreamingContent?url=http://localhost/help.gif&format=image_gif
  • http://server/servlet/StreamingContent?url=http://localhost/help.pdf&format=pdf
  • http://server/servlet/StreamingContent?url=http://localhost/help.doc&format=msdownload
  • http://server/servlet/StreamingContent?url=http://localhost/help.html&format=html
  • http://server/servlet/StreamingContent?url=http://localhost/help.xml&format=xml
  • Don’t Count Only on PrintWriter
    Use this solution whenever you need a HTTP servlet to stream various types of document that are both binary and character data, such as HTML, XML, PDF, Word, spreadsheet, and PowerPoint. Don’t assume that PrintWriter will stream binary and character data; it won’t always do it.

    Share the Post:
    Heading photo, Metadata.

    What is Metadata?

    What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

    XDR solutions

    The Benefits of Using XDR Solutions

    Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

    AI is revolutionizing fraud detection

    How AI is Revolutionizing Fraud Detection

    Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

    AI innovation

    Companies Leading AI Innovation in 2023

    Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

    data fivetran pricing

    Fivetran Pricing Explained

    One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

    kubernetes logging

    Kubernetes Logging: What You Need to Know

    Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes