cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Your Friends List is a way to easily have access to the community members that you interact with the most! X

Streaming back binary content

avillanueva
22-Sapphire II

Streaming back binary content

This topic has come up in many forms, mainly for methods to download PDFs or Excel files from PDMLink. This post does not deal with the mechanics of generating or retrieving the data to be returned but rather the methods to properly structure the action in PDMLink. The customizer's guide has good instructions for creating a JSP page and hooking that into an action. I have that down pretty good. Problem is, JSP from what I've read is not suited for streaming back binary content. Its setup for text/HTML. Yes, it can be changed and I think I have it working pretty well but I consider it a stroke of luck that it works.

There are some cases where I've see the exception "java.lang.IllegalStateException: OutputStream already obtained" returned. I believe this is where the JSP response started writing stuff back as text and I tried to re-grab the stream as xls and write to it.
Now, I mentioned that I think I've worked around it and am still using a JSP page...
<%@ page import="wt.fc...%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page contentType="application/xls" %>
...
excelUtil.createExcelFile(response.getOutputStream(),sheet1,sheet2);
response.reset();
response.setHeader("Pragma","cache");
response.setHeader("Cache-Control","private");
response.setHeader("Content-disposition","attachment; filename="+filename+".XLS");//should prompt for download
excelUtil.writeFile();
...
catch(Exception e)
{
response.setContentType("text/html");
out.println(e.getMessage());
}

But, if I still see this error, I will be forced to rewrite as servlet. Does anyone have an example how the action configuration changes to use a servlet as opposed to JSP page? Would I use a pure servlet or some extension of a API class like DefaultObjectFormProcessorDelegate? I fear I am straying back into template processor land which is not supported anymore.

Antonio Villanueva - Sr. Software Engineer - ISR Systems
UTC AEROSPACE SYSTEMS
100 Wooster Heights Road, Danbury, CT 06804
Tel: +1 203 797 5682
antonio.villanueva@utas.utc.com<">mailto:antonio.villanueva@utas.utc.com> www.utcaerospacesystems.com
CONFIDENTIALITY WARNING: This message may contain proprietary and/or privileged information of UTC Aerospace Systems and its affiliated companies. If you are not the intended recipient please 1) do not disclose, copy, distribute or use this message or its contents, 2) advise the sender by return e-mail, and 3) delete all copies (including all attachments) from your computer. Your cooperation is greatly appreciated.

2 REPLIES 2

hi,

this does work if you make sure to not have *any* dangling whitespace:

do *not* use

<%@ page import="wt.fc…%>

<%@ page trimDirectiveWhitespaces="true" %>

<%@ page contentType="application/xls" %>



rather use

<%@ page import="wt.fc…%><%@
page trimDirectiveWhitespaces="true" %><%@
page contentType="application/xls" %><%

… code here ...

if(true)return; // else we send the newline below 😉
%>

hth, martin

jessh
12-Amethyst
(To:avillanueva)

Using a servlet is rather trivial.

You can still do preliminary stuff in your JSP as well if that works out
better for you -- that way you don't have to change the URL seen at the
browser/request level either.

For instance, I have various charting JSP pages where I do

<jsp:forward page="/servlet/JFreeChartServlet"/">

as the very last line of the JSP -- after setting up data in request
attributes for the servlet to work on in my JSP. [In this case this
also enormously helpful since the JFreeChartServlet is quite reusable,
so I can just have a common body of code take over from there for
numerous disparate JSP pages.]

Announcements


Top Tags