[opencms-dev] Setting Content Disposition and Content Type

Olli Aro olli_aro at yahoo.co.uk
Mon Mar 14 09:32:58 CET 2005


Hi Brett,

It used to be with version 5 anyway that you had to use the response object
from requestContext if you wanted to write the HTTP headers.

You can get the response object with:

I_CmsResponse response=cms.getRequestContext().getResponse();

Regards,

Olli

-----Original Message-----
From: opencms-dev-bounces at opencms.org
[mailto:opencms-dev-bounces at opencms.org] On Behalf Of Brett Beaumont
Sent: 14 March 2005 00:46
To: The OpenCms mailing list
Subject: RE: [opencms-dev] Setting Content Disposition and Content Type

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();
  }



_______________________________________________
This mail is send to you from the opencms-dev mailing list
To change your list options, or to unsubscribe from the list, please visit
http://mail.opencms.org/mailman/listinfo/opencms-dev

-- 
No virus found in this incoming message.
Checked by AVG Anti-Virus.
Version: 7.0.308 / Virus Database: 266.7.1 - Release Date: 09/03/2005
 

-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.308 / Virus Database: 266.7.1 - Release Date: 09/03/2005
 





More information about the opencms-dev mailing list