Re: Trigger Downloaddialog

From: Michael Borgwardt (brazil_at_brazils-animeland.de)
Date: 10/14/03


Date: Tue, 14 Oct 2003 14:38:56 +0200

Frank Lorenz wrote:
> Hi
>
> A servlet generated response is treaten differently each time. One time the
> webbrowser raises correctly a download dialog, wich enables the user to save
> a file. This happens when the data to download is small. The other time,
> instead to show a download dialog, the webbrowser shows the data directly as
> a new page. This happens with larger downloaddata.
>
> Therefore i suppose it depends on wrong caching adjustments i've made in the
> responseheader.

Close but not quite. You must set the header data *before* writing the the output
stream, because when the output buffer is filled the servlets begins to send the
reply and any headers set afterwards are ignored.

You have another bug waiting to happen:

> String csv_export = (String)aRequest.getSession().getAttribute(ATTR_KEY);
> size = csv_export.length();
>
> out = new ByteArrayOutputStream(size);
> out.write(csv_export.getBytes());
> out.writeTo(aResponse.getOutputStream());
> out.flush();
> out.close();
> aResponse.setContentLength(size);

If the platform default encoding ever happens to be one that uses more than one byte
for some characters (such as UTF-8) and such characters occur, your size value will be
wrong and the download may be truncated. Thus, you should specify the encoding explicitly
AND not rely on it using one byte per character (in case you ever want to change it).
Do it like this:

      private static final String CHARSET = "ISO-8859-1";

          ...

          out = new ByteArrayOutputStream(csv_export.length());
          out.write(csv_export.getBytes(CHARSET));
          out.flush();

          // other headers here
          aResponse.setContentLength(out.size());
          aResponse.setContentType("text/comma-separated-values; charset=" + CHARSET);

          out.writeTo(aResponse.getOutputStream());
          out.close();



Relevant Pages

  • Trigger Downloaddialog
    ... A servlet generated response is treaten differently each time. ... webbrowser raises correctly a download dialog, wich enables the user to save ... This happens when the data to download is small. ...
    (comp.lang.java.programmer)
  • Re: Office 2003 SP2
    ... > explicitly inquired about someone else downloading the patch for you, ... My response to you indicated that this would not be legal. ... > converation about whether it is legal or not to ask someone else to download ... install the update onto my PC from that CD? ...
    (microsoft.public.officeupdate)
  • Raising Download Dialog
    ... A servlet generated response is treaten differently each time. ... webbrowser raises correctly a download dialog, wich enables the user to save ... This happens when the data to download is small. ... The header is configured ...
    (comp.lang.java.programmer)
  • Re: [unix, console] : something like wget but with forms + authentification
    ... >> download the response after submitting your name and password, ... > tools, like lynx, links, elinks, wget, don't seem to access to this ... I used wget to download the pages you linked to, ... html expert, but perhaps I have been exposed to a little more ...
    (comp.unix.shell)
  • Re: iMac 24" Installed Software
    ... the box) CD/DVD, a CD/DVD that will arrive in the mail, or via an ... And you seem to believe that all software comes with the Mac ... because you can download it to a Windows computer. ... If you can sit there and read JJ's post, Mike's response, and then Gerry's ...
    (comp.sys.mac.system)