Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home Answers Viewqa JSP-Servlet Servletoutputstream and Bytearrayoutputstream

Have Programming Question? Ask it here!

ratna rathor
Servletoutputstream and Bytearrayoutputstream
2 Answer(s)      2 years and 3 months ago
Posted in : JSP-Servlet

How to send data to client in binary form using ServletOutputStream?

View Answers

May 22, 2012 at 6:34 PM


import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                String filePath = "D:\\ratna\\";
        String fileName = "";
        FileInputStream fileToDownload = new FileInputStream(filePath);
        ServletOutputStream output = response.getOutputStream();
        response.setHeader("Content-Disposition", "attachment; filename="
                + fileName);

        ByteArrayOutputStream output1 = new ByteArrayOutputStream(); 
        int readBytes = 0;
        byte[] buffer = new byte[10000];
        while ((readBytes =, 0, 10000)) != -1) {
            output.write(buffer, 0, readBytes);
            output1.write(buffer, 0, readBytes);


Description: - Servletoutputstream is used for sending data to client in binary format. It is retrieved via response.getOutputStream() function. ServletOutputStream is an abstract class and servlet container implements it. When you use character conversion and there is a lot of text then it is better to use byte[] instead of String object. ByteArrayOutputStream implements output stream and it contain data in form of byte array.For retrieving data you can use toByteArray() and toString() methods.

March 21, 2013 at 2:55 PM

I need retrieve byte array in servlet ,could you explain me that?

Related Tutorials/Questions & Answers:
Servletoutputstream and Bytearrayoutputstream
Servletoutputstream and Bytearrayoutputstream  How to send data to client in binary form using ServletOutputStream
Servletoutputstream size limit.
Servletoutputstream size limit.  What is the maximum size of ServletOutputStream
Servletoutputstream size limit.
Servletoutputstream size limit.  What is the maximum size of ServletOutputStream?   By default size is set to 10MB.You can increase your message size maximum 2000000000 bytes. That is size limit is 2000000000
conver byte array to ByteArrayOutputStream
conver byte array to ByteArrayOutputStream  Sir Can you please tell me how to convert the byte[] to ByteArrayOutputStream; One of my method will return the byte[] , i have to convert this byte array to ByteArrayOutputStream
Reading file into bytearrayoutputstream
How to read file into bytearrayoutputstream This tutorial shows you how... bytearrayoutputstream. This example code is very helpful in learning the concept of byte... into bytearrayoutputstream. This example will teach you how you can read
Java ByteArrayOutputStream Example
Java ByteArrayOutputStream Example In this section we will discuss about the Java IO Byte Streams ByteArrayOutputStream. ByteArrayOutputStream...() method does not put any effect on the ByteArrayOutputStream, methods can
Java - Java Server Faces Questions
"; ServletOutputStream out = response.getOutputStream...; ByteArrayOutputStream output = new ByteArrayOutputStream(); int
Display PDF in browser - Struts
(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter.getInstance(document..."); response.setContentLength(baos.size()); ServletOutputStream out
JSP - Java Beginners
JSP  how to use this code in no response object. ServletOutputStream outStream=response.getOutputStream(); response.setContentType("image/jpg"); response.setContentLength((int)fileLength
Crop Image using plug-in ,JSP and 'Servlet'
); ByteArrayOutputStream out=new ByteArrayOutputStream(); ImageIO.write(cropped...;); ServletOutputStream wrt=res.getOutputStream(); wrt.write(out.toByteArray
Java read entire file into byte array
complete example at Reading file into bytearrayoutputstream Thanks
Java convert file to byte array
. Read the complete examples at: Reading file into bytearrayoutputstream Reading
Exception - JSP-Servlet
Exception  Hi, I am Ragavendran.R.. I am using swiftchart for my chart application.. For this purpose, I am using ServletOutputStream sos=response.getOutputStream(); in my JSP Page. But, I am getting
Retriving zip file stored in database into my own filesystem
)); //ByteArrayOutputStream byt = new ByteArrayOutputStream(1024
Java writer outputstream
is used. And subclasses ByteArrayOutputStream & FileOutputStream...;new ByteArrayOutputStream();   OutputStreamWriter ow 
Image Processing Java
will be a ByteArrayOutputStream (in memory) ByteArrayOutputStream bos = new ByteArrayOutputStream... the ByteArrayOutputStream create a RenderedImage. ByteArrayInputStream in = new
Capturing JSP Content Within My Strut Action Servlet - JSP-Servlet
WebLogic is the explicit population of the ByteArrayOutputStream. I am not sure...; ByteArrayOutputStream baos = null; /** * Constructor... explicit population of the ByteArrayOutputStream here (I am relearning Streams
Servlets - Java Interview Questions
{ response.setContentType("text/html"); ServletOutputStream out
How to insert clob data??
; ByteArrayOutputStream output = new ByteArrayOutputStream(); int length; byte
java - Java Beginners
(); // Serialize to a byte array ByteArrayOutputStream baos = new ByteArrayOutputStream
download - JSP-Servlet
buf=null; ServletOutputStream myOut=null; try...; //read from the file; write to the ServletOutputStream while...(); System.out.println("inside download servlet"); ServletOutputStream myOut
Using Spring mock objects
implementation of the ServletOutputStream interface. Whenever required to check
how to draw lines,circles, rectangles on JSP (using Java Graphics)
image ServletOutputStream sos = response.getOutputStream(); JPEGImageEncoder
javas - JSP-Servlet
{ ServletOutputStream out = response.getOutputStream(); String connectionURL
pass parameter - file - Java Beginners
, IOException{ ServletOutputStream out = response.getOutputStream
upload and download all kinds of files in jsp - Java Beginners
().length()); InputStream in = new FileInputStream(f); ServletOutputStream
Pass parameters from JSP to Servlet
{ ServletOutputStream out = res.getOutputStream(); String name
Java ServletResponse Interface
data in MIME body response then you have to use ServletOutputStream returned... returns a ServletOutputStream suitable for writing binary data in the response
Audio Level Meter
running; ByteArrayOutputStream out; public Capture() { super...() { out = new ByteArrayOutputStream(); running = true
String Compression using Deflater class in java
for compression. The ByteArrayOutputStream class create output stream ...;ByteArrayOutputStream(         
Insert Serialized Object into Database Table
{ ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oout = new
Serializing Objects Into Database - java tutorials
it to a ByteArrayOutputStream via an ObjectOutputStream, convert the ByteArrayOutputStream to a byte array and then call the setBytes method on the prepared statement... SQLException, IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream
Java File - Learn how to handle files in Java with Examples and Tutorials
into bytearrayoutputstream Reading binary file into byte array in Java How
Java writer api
the parentheses you can pass the object of ByteArrayOutputStream class object. Syntax with examples for using the class:-  ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStreamWriter rachel = new OutputStreamWriter(out
how to display csv file directly in IE browser .
;TableController> iter = list.iterator(); ServletOutputStream out = null
upload and download a file - Java Beginners
); ServletOutputStream outs = response.getOutputStream(); int bit = 256
J2EE - Development process
. It can only write information to the ServletOutputStream or Writer
how to call jsp variable through servlet
in = new FileInputStream(f); ServletOutputStream outs = response.getOutputStream
JSP file download - JSP-Servlet
to download word file: <% ServletOutputStream output
= null; ServletOutputStream out = response.getOutputStream(); try
JSP Code - Java Beginners
FileInputStream(f); ServletOutputStream outs = response.getOutputStream
. This method can be called at any time. It can only use ServletOutputStream or Writer
Getting Context Parameter Names
req, HttpServletResponse res) throws IOException { ServletOutputStream
required treenodes checkboxes are not clicking if u change the url - Java Server Faces Questions
"); ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] rb = new byte[1024
Need to expand tree by passing Id thr URL. (not by clicking manually) - Java Server Faces Questions
"); ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] rb = new byte[1024
Need to expand tree by passing Id thr URL. - Java Server Faces Questions
().getResourceAsStream("org/richfaces/demo/tree/data.txt"); ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] rb = new byte[1024]; int read
Enum Inversion Problem
); } } We can use this by creating a ByteArrayOutputStream, then write...) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream
IllegalStateException for file opening
); ServletOutputStream outs = response.getOutputStream(); int bit = 256...); ServletOutputStream outs = response.getOutputStream(); int bit = 256; int
Post Message In servlet
it as an argument to the servlet's service method. ServletOutputStream.... A ServletOutputStream object is created using  response.getOutputStream