views:

314

answers:

1

I think is it quite possible, but I'm not sure.

I don't have the possibility to use servlet directly, so I'm forced to use JSP ( long history, short time, you don't want to hear )

So I think something like the following will do:

// PSEUDO-CODE:
// source.jsp
Download your file
<a href="file.jsp?xyz">MyDocument.doc</a>


// file.jsp
<%@page content-type="pplicaton/somethig-binary-xyz"%>
byte[] data = getBinaryFromSomeWhere();

int start = 0;
int end = data.length < 1024 ? data.length : 1024;
int written = 0;
while( written < data.length ) {
    out.write( data, start, end );
    writtern += end;
    start = end;
    end += written + data.length < 1024 ? data.length : 1024;
}

%>

Don't put too much attention to the code. It only shows the idea. It writes the bynary array to the jsp output stream.

Is it possible? Does it sounds reasonable? Is there a JSTL or other thing that already handles that?

+3  A: 

Yes, use "application/octet-stream" for generic binary data. And remove every line break/whitespace from the import tags and around the scriptlets.

<%@page content-type="applicaton/octet-stream"%><%
byte[] data = getBinaryFromSomeWhere(request.getParameter("xyz"));
request.setHeader("Content-length", Integer.toString(data.length));
out.write( data, start, end );
out.flush();
%>
kd304