Saturday, March 30, 2013

ServletOutputStream Class in java

It is  an abstract class . which is provide stream to write binary data into the response

  ServletOutputStream fout = response.getOutputStream()



Example :   to down load video in jsp page


String url = request.getParameter("url");
    String title = request.getParameter("title");
    URL tU = new URL(url);
    HttpURLConnection conn = (HttpURLConnection) tU.openConnection();
    String type = conn.getContentType();
    InputStream ins = conn.getInputStream();
    ServletOutputStream fout = response.getOutputStream();
    response.setContentType("mime/flv");
    response.setHeader("Content-disposition","attachment; filename=\""+title+".flv\"");
    byte[] outputByte = new byte[conn.getContentLength()];
    int bytesRead;
    int length = conn.getContentLength();
    int read = 0;
    while((bytesRead = ins.read(outputByte, 0, length)) != -1){
         //read += bytesRead;
         //System.out.println(read + " out of " + length);
         fout.write(outputByte, 0, bytesRead);
    }
     fout.flush();
     fout.close();
     out.clear(); // where out is a JspWriter
     out = pageContext.pushBody();
     response.getOutputStream().flush();



No comments:

Post a Comment