[opencms-dev] Setting Content Disposition and Content Type
Brett Beaumont
brett.beaumont at sytec.co.nz
Mon Mar 14 01:45:33 CET 2005
Thanks Thomas,
I'm still not having much luck. What I'm trying to do is create a page that allows me to download files from the WEB-INF/export folder. My code looks something like what I've included below, but I've removed some of the error handling for clarity.
I've tried several different approaches. First I tried creating this as a JSP in the content area of OpenCMS. This sets the content type correctly, but the content gets truncated. I haven't determined a pattern exactly, but the full content does not get returned. To prove that the code itself works, I created a duplicate of the JSP in a stand-alone web-app and it worked correctly. This suggests that OpenCMS was truncating the response somehow (perhaps problems in the encoding or something like that).
Secondly, I tried moving the JSP into the modules area of OpenCMS and then creating a "page" that used this JSP as its template. This gets me the complete content, but it ignores the headers. In Mozilla, this opens the binary file in the browser. In IE, sometimes it opens in the browser, and sometimes it prompts me for download. However, even when it prompts for download, it ignores the content-disposition header (which should make the default filename be "test.zip").
This is all running on OpenCMS 5.0.0.
Is this similar to what you've achieved, and am I using the same version of OpenCMS as you are?
Brett
String filename = "/var/tomcat4/webapps/opencms/WEB-INF/export/" + request.getParameter("fname");
// Set the headers
response.setHeader("Content-Type", "application/x-download");
response.setHeader("Content-Disposition", "attachment; filename=test.zip");
// Get the HTTP Response output stream.
OutputStream outstr = response.getOutputStream();
// Read the file and stream it to the browser.
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(filename));
byte[] buf = new byte[4 * 1024]; // 4K char buffer
int bytesRead;
while ((bytesRead = in.read(buf, 0, 4 * 1024)) != -1) {
System.err.println("Read: " + bytesRead);
outstr.write(buf, 0, bytesRead);
}
outstr.flush();
out.flush();
}
catch(Exception ex) {
// Do some error handling
}
finally {
if (in != null) in.close();
}
More information about the opencms-dev
mailing list