how to download binary file using jsp at client side

Discussion in 'Java' started by Girish T Deshpande, Jul 1, 2003.

  1. I am using Tomcat 4 and wish to download binary file(actualy an excel
    and pdf file) to the local disk.
    Since out.write ouputs the character array, I get error message when I
    pass byte array.

    The part of the code is below(I have set proper mime type)

    FileReader f = new FileReader("C:/Tengli/jakarta-tomcat-4.1.24/webapps/ROOT/JavaExcel/ouput9.xls");

    BufferedInputStream inputStream = new BufferedInputStream(new
    FileInputStream("C:/Tengli/jakarta-tomcat-4.1.24/webapps/ROOT/JavaExcel/ouput9.xls"));

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    while (true) {
    int nBytes = inputStream.read(buffer);
    if (nBytes < 0) break;
    out.write(buffer, 0, nBytes);
    }
    //buffer = out.toByteArray();


    I also get the error that out is already defined in
    _jspService(......).
    If I use other variable name instead of out it compiles and runs fine
    but the file will be completely empty since it writes at unknown
    place.


    Could anyone give the entire code!

    Thank you
    Girish T Deshpande, Jul 1, 2003
    #1
    1. Advertising

  2. Girish T Deshpande

    Sergio Juan Guest

    Hi

    "out" is a predefined object in the jsp (it is a Writer to
    response.getOutputStream()) so use another name.
    ITOH, the code looks fine.

    Regards.

    "Girish T Deshpande" <> escribió en el mensaje
    news:...
    > I am using Tomcat 4 and wish to download binary file(actualy an excel
    > and pdf file) to the local disk.
    > Since out.write ouputs the character array, I get error message when I
    > pass byte array.
    >
    > The part of the code is below(I have set proper mime type)
    >
    > FileReader f = new

    FileReader("C:/Tengli/jakarta-tomcat-4.1.24/webapps/ROOT/JavaExcel/ouput9.xl
    s");
    >
    > BufferedInputStream inputStream = new BufferedInputStream(new
    >

    FileInputStream("C:/Tengli/jakarta-tomcat-4.1.24/webapps/ROOT/JavaExcel/oupu
    t9.xls"));
    >
    > ByteArrayOutputStream out = new ByteArrayOutputStream();
    > byte[] buffer = new byte[1024];
    > while (true) {
    > int nBytes = inputStream.read(buffer);
    > if (nBytes < 0) break;
    > out.write(buffer, 0, nBytes);
    > }
    > //buffer = out.toByteArray();
    >
    >
    > I also get the error that out is already defined in
    > _jspService(......).
    > If I use other variable name instead of out it compiles and runs fine
    > but the file will be completely empty since it writes at unknown
    > place.
    >
    >
    > Could anyone give the entire code!
    >
    > Thank you
    Sergio Juan, Jul 2, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lau Lei Cheong
    Replies:
    1
    Views:
    618
    Lau Lei Cheong
    Oct 13, 2004
  2. =?Utf-8?B?dmlkeWE=?=
    Replies:
    1
    Views:
    748
    Kevin Spencer
    Jun 2, 2005
  3. Boss302
    Replies:
    0
    Views:
    1,035
    Boss302
    Nov 21, 2006
  4. Bogdan
    Replies:
    2
    Views:
    644
    Bogdan
    Jun 9, 2008
  5. Zoe Hart
    Replies:
    1
    Views:
    364
    Scott Wisniewski
    Jan 8, 2004
Loading...

Share This Page