[opencms-dev] Can not override mimetype from jsp

Paul-Inge Flakstad flakstad at npolar.no
Fri May 24 12:19:20 CEST 2013


Hi again Yves,

Did you call it before calling response.setContentType() ? If I remember correctly, it’s important you do.

I remember I had the same issue (long time ago now), but I eventually got it working. As far as I can tell from my code, it was cms.setContentType() that did the trick.

Here’s a copy-paste from my JSP:

byte[] rawContent = csvContent.getBytes(Charset.forName("UTF-8"));

                cms.setContentType("text/csv;charset=UTF-8");
                response.setContentType("text/csv;charset=UTF-8");
                response.setContentLength(rawContent.length);
                response.setHeader("Content-Disposition","attachment; filename=\"data.csv\"");

                response.getOutputStream().write(rawContent, 0, rawContent.length);
                response.getOutputStream().flush();

I’ve used this on a range of OpenCms versions (part of a module), and it works. Not tested on v8.5 though.

Cheers,
Paul

From: opencms-dev-bounces at opencms.org [mailto:opencms-dev-bounces at opencms.org] On Behalf Of Yves Glodt
Sent: 24. mai 2013 12:02
To: The OpenCms mailing list
Subject: Re: [opencms-dev] Can not override mimetype from jsp

Thanks for the replies so far !

@Paul: I added          CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response); cms.setContentType((metadata != null) ? metadata.get(Metadata.CONTENT_TYPE) : "application/octet-stream");
but nothing changed, still text/html

@Siegfried: The download-jsp was called getFile.jsp. I renamed it to download (without any extension) but that did also not help :(


before: /getFile.jsp?file=/COMMON/bla/2013_02_DE.pdf&name=2013_02_DE.pdf
after: /download?file=/COMMON/bla/2013_02_DE.pdf&name=2013_02_DE.pdf


On 24 May 2013 11:19, Siegfried Ehret <s.ehret at eurelis.com<mailto:s.ehret at eurelis.com>> wrote:
Hey,

You may also check what is the default mime type for your file extension in WEB-INF/config/opencms-vfs.xml
As an example, all files ending with “.html” will always have the “text/html” mime type:
<mimetypes>
…
<mimetype extension=".html" type="text/html" />
…
</mimetypes>

Regards,

Siegfried Ehret

De : opencms-dev-bounces at opencms.org<mailto:opencms-dev-bounces at opencms.org> [mailto:opencms-dev-bounces at opencms.org<mailto:opencms-dev-bounces at opencms.org>] De la part de Paul-Inge Flakstad
Envoyé : vendredi 24 mai 2013 11:10
À : The OpenCms mailing list
Objet : Re: [opencms-dev] Can not override mimetype from jsp

Hi Yves,

Try calling CmsJspActionElement.setContentType(java.lang.String) as well. I see I’ve used that method in my similar JSPs, and they work like they should.

HTH :)

Cheers,
Paul

From: opencms-dev-bounces at opencms.org<mailto:opencms-dev-bounces at opencms.org> [mailto:opencms-dev-bounces at opencms.org] On Behalf Of Yves Glodt
Sent: 24. mai 2013 11:03
To: The OpenCms mailing list
Subject: [opencms-dev] Can not override mimetype from jsp

Hello,

I have a JSP which reads a file from the local filesystem (not the vfs) and outputs it to the browser.

But the content-type I set is not taken into account. Whatever I tried the browser always gets text/html as content-type. Does OpenCms override it ?




Here is a code fragment:


            ServletOutputStream outStream=response.getOutputStream();
            File myfile = new File("/mnt/data/"+filename);
            FileInputStream input = new FileInputStream(myfile);

            System.out.println(request.getRemoteAddr() + ": Size: " + myfile.length());

            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition","inline;filename=" + name);
            response.setContentLength( (int) myfile.length() );


            int readBytes = 0;
            while((readBytes = buf.read()) != -1) {
                        outStream.write(readBytes);
            }
            outStream.flush();
            outStream.close();
            buf.close();

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




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://webmail.opencms.org/pipermail/opencms-dev/attachments/20130524/daa519fa/attachment.htm>


More information about the opencms-dev mailing list