Browse DevX
Sign up for e-mail newsletters from DevX


Stream Both Character and Binary Data via a Servlet : Page 3

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.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 = bis.read(buff, 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 * <p> * @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 = bis.read(buff, 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 StreamingContent.java 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 <servlet> 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.

    Raj Behera is a technical specialist with Reuters in New York. He has worked extensively with Java, Visual Basic, C#, J#, JSP, ASP, C, and COM for years, developing real-time trading systems, news systems, and financial applications. Reach him at rkbehera@yahoo.com.
    Comment and Contribute






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



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