Servletoutputstream and Bytearrayoutputstream



Share on Google+Share on Google+

ratna rathor
Servletoutputstream and Bytearrayoutputstream
2 Answer(s)      5 years and 5 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
Java writer outputstream
is used. And subclasses ByteArrayOutputStream & FileOutputStream...;new ByteArrayOutputStream();   OutputStreamWriter ow 
Retriving zip file stored in database into my own filesystem
)); //ByteArrayOutputStream byt = new ByteArrayOutputStream(1024
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
java - Java Beginners
(); // Serialize to a byte array ByteArrayOutputStream baos = new ByteArrayOutputStream
How to insert clob data??
; ByteArrayOutputStream output = new ByteArrayOutputStream(); int length; byte
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
Pass parameters from JSP to Servlet
{ ServletOutputStream out = res.getOutputStream(); String name
upload and download all kinds of files in jsp - Java Beginners
().length()); InputStream in = new FileInputStream(f); ServletOutputStream
pass parameter - file - Java Beginners
, IOException{ ServletOutputStream out = response.getOutputStream
javas - JSP-Servlet
{ ServletOutputStream out = response.getOutputStream(); String connectionURL
how to draw lines,circles, rectangles on JSP (using Java Graphics)
image ServletOutputStream sos = response.getOutputStream(); JPEGImageEncoder
Using Spring mock objects
implementation of the ServletOutputStream interface. Whenever required to check
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
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
Audio Level Meter
running; ByteArrayOutputStream out; public Capture() { super...() { out = new ByteArrayOutputStream(); running = true


Advertisement null