Trigger Downloaddialog

F

Frank Lorenz

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. But i'm not really shure about it. The header is configured
in a way, that the response is declarated as extreme volatile, so that the
caching mechanism actually will not grip.

The problem occurs with any webbrowser (doesn't matter if ie, ns,
mozilla...). Tomcat 4.0.x is used.

Regards Frank

--
public void doGet(HttpServletRequest aRequest, HttpServletResponse
aResponse)
throws IOException, ServletException {

try
{
int size;
ByteArrayOutputStream out;
String csv_export =
(String)aRequest.getSession().getAttribute(ATTR_KEY);

if((csv_export != null) && (csv_export.trim().length() > 0))
{
size = csv_export.length();

out = new ByteArrayOutputStream(size);
out.write(csv_export.getBytes());
out.writeTo(aResponse.getOutputStream());
out.flush();
out.close();

// Response-Header Setup
aResponse.setHeader("Content-Disposition","attachment; filename=" +
FILENAME + "; size=" + size);
aResponse.setContentLength(size);
aResponse.setContentType("text/comma-separated-values");
aResponse.addDateHeader("Expires:", (System.currentTimeMillis()+
1000*60*2));

aRequest.getSession().removeAttribute(ATTR_KEY);
}
else
{
aRequest.getSession().removeAttribute(ATTR_KEY);
throw new Exception("No Data");
}
}
catch (Exception e)
{
e.printStackTrace();
aRequest.getSession().removeAttribute(ATTR_KEY);
sendErrorPage(aResponse, e.getMessage());
}
}
--
 
M

Michael Borgwardt

Frank said:
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();
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top