Problem while downloading a tab delimited file to excel using Servlet

Discussion in 'Java' started by Niraj Agarwal, Jan 12, 2004.

  1. Hi,

    I am trying to download a tab-delimited file to client machine using
    servlet.
    I am using IE5.5 browser to test the application.
    Here is the code in my servlet, which is sending the file to the
    client machine.

    ServletOutputStream out = response.getOutputStream();

    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-Disposition", "inline;
    filename=\""+getFileNameOnly()+".xls\"");

    // Generate tab delimited string buffer
    StringBuffer sbRet = generateXlsFormat();

    // Send it to client
    out.write(sbRet.toString().getBytes());

    out.close();

    I want to download (open) this file directly to a new browser window,
    or in excel with the filename i am passing from the servlet.

    The problem is that, it opens the file in excel but the file name is
    the url that i posted to get this file, it is not that i set in
    servlet.

    I tried all permutations and combinations but nothing is working.

    Can someone please help me out to resolve this problem?

    Thanks in advance,
    Niraj
    Niraj Agarwal, Jan 12, 2004
    #1
    1. Advertising

  2. (Niraj Agarwal) wrote in message news:<>...
    > Hi,
    >
    > I am trying to download a tab-delimited file to client machine using
    > servlet.
    > I am using IE5.5 browser to test the application.
    > Here is the code in my servlet, which is sending the file to the
    > client machine.
    >
    > ServletOutputStream out = response.getOutputStream();
    >
    > response.setContentType("application/vnd.ms-excel");
    > response.setHeader("Content-Disposition", "inline;
    > filename=\""+getFileNameOnly()+".xls\"");
    >
    > // Generate tab delimited string buffer
    > StringBuffer sbRet = generateXlsFormat();
    >
    > // Send it to client
    > out.write(sbRet.toString().getBytes());
    >
    > out.close();
    >
    > I want to download (open) this file directly to a new browser window,
    > or in excel with the filename i am passing from the servlet.
    >
    > The problem is that, it opens the file in excel but the file name is
    > the url that i posted to get this file, it is not that i set in
    > servlet.
    >
    > I tried all permutations and combinations but nothing is working.
    >
    > Can someone please help me out to resolve this problem?
    >
    > Thanks in advance,
    > Niraj



    We do virtually the same thing and it works. The only differences I
    see
    are:

    1) We use "application/x-ms-excel" for the content-type.
    2) We set the headers "Cache-Control" and "Pragma" to "".
    3) Our "Content-disposition" header has the lower-case "d".
    4) We set the content length a la:
    response.setContentLength(sbRet.toString().getBytes().length);
    5) We flush the output after writing it a la:
    response.getOutputStream().flush();

    See if one or more of the above fixes your problem.

    Greg
    Gregory A. Swarthout, Jan 12, 2004
    #2
    1. Advertising

  3. Thanks Greg,

    I tried all things you mentioned, but it is still the same. Can you
    share your code with me if possible. Also can it be the problem with
    AppServer. I am using WebLogic 8.1.

    Also when i use "Content-disposition-type" as "attachment" instead of
    "inline", it shown "Save As" dialog, but if i use "open from current
    location" option it ask for 3 times before opening the file in excel,
    but this time it uses the right file name.

    Thanks,
    Niraj

    (Gregory A. Swarthout) wrote in message news:<>...
    > We do virtually the same thing and it works. The only differences I
    > see
    > are:
    >
    > 1) We use "application/x-ms-excel" for the content-type.
    > 2) We set the headers "Cache-Control" and "Pragma" to "".
    > 3) Our "Content-disposition" header has the lower-case "d".
    > 4) We set the content length a la:
    > response.setContentLength(sbRet.toString().getBytes().length);
    > 5) We flush the output after writing it a la:
    > response.getOutputStream().flush();
    >
    > See if one or more of the above fixes your problem.
    >
    > Greg
    Niraj Agarwal, Jan 13, 2004
    #3
  4. Thanks Greg,

    I tried all the things you mentioned but still the result is the same,
    file opened in excel does not have right file name. Can you share your
    code that is working fine, if you can. Can it be problem with
    Application server, we are using Weblogic 8.1.

    Niraj


    (Gregory A. Swarthout) wrote in message news:<>...
    >
    > We do virtually the same thing and it works. The only differences I
    > see
    > are:
    >
    > 1) We use "application/x-ms-excel" for the content-type.
    > 2) We set the headers "Cache-Control" and "Pragma" to "".
    > 3) Our "Content-disposition" header has the lower-case "d".
    > 4) We set the content length a la:
    > response.setContentLength(sbRet.toString().getBytes().length);
    > 5) We flush the output after writing it a la:
    > response.getOutputStream().flush();
    >
    > See if one or more of the above fixes your problem.
    >
    > Greg
    Niraj Agarwal, Jan 13, 2004
    #4
    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. Replies:
    5
    Views:
    4,827
  2. RyanL
    Replies:
    6
    Views:
    685
    Paul McGuire
    Aug 28, 2007
  3. Gibson
    Replies:
    2
    Views:
    1,403
    Gibson
    Nov 19, 2008
  4. Replies:
    1
    Views:
    329
    ZedGama3
    Apr 14, 2004
  5. Srikant
    Replies:
    1
    Views:
    492
    Greg Bacon
    Sep 29, 2007
Loading...

Share This Page