Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date