Download excel file from server using jsp - Weblogic

This is a discussion on Download excel file from server using jsp - Weblogic ; Hi, I am trying to download excel file which is on server using jsp. here is code which I wrote:- String reportName = "test"; String sBrowser = ""+request.getHeader("User-Agent") ; boolean bIE5 = (sBrowser.toUpperCase().indexOf("MSIE 5")!=-1) ; com.gymboree.frontend.util.Debug.print("bIE55 = " + bIE5); ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Download excel file from server using jsp

  1. Download excel file from server using jsp


    Hi,

    I am trying to download excel file which is on server using jsp.

    here is code which I wrote:-

    <%
    String reportName = "test";

    String sBrowser = ""+request.getHeader("User-Agent") ;
    boolean bIE5 = (sBrowser.toUpperCase().indexOf("MSIE 5")!=-1) ;
    com.gymboree.frontend.util.Debug.print("bIE55 = " + bIE5);
    if (bIE5)
    {
    response.setContentType("application/vnd.ms-excel") ;

    response.setHeader("Content-Disposition", "attachment; filename="+ reportName
    + "3.XLS\";") ;

    response.setHeader("Pragma","no-cache");
    }
    else
    {
    response.setContentType("x-application/vnd.ms-excel");

    response.setHeader("Content-Disposition", "attachment; filename="+reportName+"1.XLS");
    response.setHeader("Pragma","no-cache");

    }

    System.out.println("start....");
    ServletOutputStream stream = response.getOutputStream();
    try
    {


    File url = new File ( "output.xls" );

    InputStream streamIn = new FileInputStream(url);
    int data = 0;
    while((data = streamIn.read()) != -1)
    {
    stream.write(data);
    }
    streamIn.close();

    stream.flush();
    stream.close();
    }
    catch (Exception ex)
    {
    ex.printStackTrace();
    }

    System.out.println("done....");
    %>

    But when I am trying to access this jsp from IE6 browser excel file is opeing
    in ms excel but it is not showing correct data. It's just showing junk characters.

    thanks,

    jai

  2. Re: Download excel file from server using jsp

    Hi

    Is your program a portlet or not?
    If not, you cant try codes as following
    ===========================================

    <%@ page language="java" contentType="text/html" import="java.util.*, java.io.*, java.net.URLEncoder, javax.portlet.ActionResponse"%>
    <%
    String src_fname = "c:/temp/testdata.txt";

    response.setContentType("application/octet-stream");

    if (request.getHeader("User-Agent").indexOf("MSIE 5.5")
    != -1)
    {
    response.setHeader("Content-Disposition",
    "filename="+ src_fname);
    }
    else{
    response.addHeader( "Content-Disposition",
    "attachment;filename="+ src_fname);
    }
    FileInputStream fis = null;
    int byteRead;
    try{
    fis = new FileInputStream(src_fname);
    while((byteRead = fis.read()) != -1)
    out.write(byteRead);
    out.flush();
    }
    catch(Exception e){
    out.clearBuffer();
    response.setContentType("text/html");
    response.setHeader("Content-disposition", "inline");
    out.println("

    ");
    out.println(e.toString());
    out.println("

    ");
    }
    if(fis != null)
    fis.close();
    return;
    %>

  3. Re: Download excel file from server using jsp

    I have the same problem here..using jsp to view the EXCEL file in the browser will show junk data..

+ Reply to Thread