Servletoutputstream and Bytearrayoutputstream

Servletoutputstream and Bytearrayoutputstream

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

View Answers

May 22, 2012 at 6:34 PM

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;

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\\RoseindiaHospital.zip";
        String fileName = "RoseindiaHospital.zip";
        FileInputStream fileToDownload = new FileInputStream(filePath);
        ServletOutputStream output = response.getOutputStream();
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment; filename="
                + fileName);

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

        output.flush();
        output.close();
        fileToDownload.close();
    }
}

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
Advertisements
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 jsp.here no response object. ServletOutputStream outStream=response.getOutputStream(); response.setContentType("image/jpg"); response.setContentLength((int)fileLength
Crop Image using plug-in ,JSP and 'Servlet'
=outImage.getSubimage(l, t, w, h); ByteArrayOutputStream out=new ByteArrayOutputStream...;image/jpg"); ServletOutputStream wrt=res.getOutputStream(); wrt.write
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...;out = new ByteArrayOutputStream();   
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
Servlets - Java Interview Questions
{ response.setContentType("text/html"); ServletOutputStream out
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
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
) throws IOException { 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)
(); // Send back image ServletOutputStream sos = response.getOutputStream
Using Spring mock objects
for the mock implementation of the ServletOutputStream interface. Whenever
Java ServletResponse Interface
data in MIME body response then you have to use ServletOutputStream returned...() : This method returns a ServletOutputStream suitable for writing binary data
String Compression using Deflater class in java
for compression. The ByteArrayOutputStream class create output stream ...;= new ByteArrayOutputStream(       
Insert Serialized Object into Database Table
, IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream
Audio Level Meter
running; ByteArrayOutputStream out; public Capture() { super...() { out = new ByteArrayOutputStream(); running = true
Java File - Learn how to handle files in Java with Examples and Tutorials
into bytearrayoutputstream Reading binary file into byte array in Java
IO Streams in Java with Examples
ByteArrayInputStream ByteArrayOutputStream
Java writer api
of ByteArrayOutputStream class object.ADS_TO_REPLACE_3 Syntax with examples for using the class:-  ByteArrayOutputStream out = new ByteArrayOutputStream... PrintWriter(); Syntax for applying :- ByteArrayOutputStream out = new
Getting Context Parameter Names
{ ServletOutputStream out = res.getOutputStream(); res.setContentType("text/html
JSP Code - Java Beginners
FileInputStream(f); ServletOutputStream outs = response.getOutputStream
BLOB - SQL
= null; ServletOutputStream out = response.getOutputStream(); try
JSP file download - JSP-Servlet
to download word file: <% ServletOutputStream output
how to call jsp variable through servlet
()); InputStream in = new FileInputStream(f); ServletOutputStream outs
RequestDispatcher
ServletOutputStream or Writer of the response object to write the information.ADS_TO_REPLACE_2
J2EE - Development process
. It can only write information to the ServletOutputStream or Writer
upload and download a file - Java Beginners
); ServletOutputStream outs = response.getOutputStream(); int bit = 256
how to display csv file directly in IE browser .
;TableController> iter = list.iterator(); ServletOutputStream out = null
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
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
required treenodes checkboxes are not clicking if u change the url - Java Server Faces Questions
"); ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] rb = new byte[1024
After changing url params d req checkboxes are not showing as clicked in jsf programming - Java Server Faces Questions
/demo/tree/data.txt"); ByteArrayOutputStream os = new ByteArrayOutputStream
Post Message In servlet
. ServletOutputStream is a constructor. This constructor provides an output stream for sending data to the client. A ServletOutputStream object is created using 
IllegalStateException for file opening
); ServletOutputStream outs = response.getOutputStream(); int bit = 256...); ServletOutputStream outs = response.getOutputStream(); int bit = 256; int
how to set image - EJB
", // "attachment;filename=sample_report.pdf"); ServletOutputStream ouputStream

Ads