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="applicaton/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?

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
OscarRyz
  • 196,001
  • 113
  • 385
  • 569

1 Answers1

2

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

<%@ page contentType="applicaton/octet-stream" %><%
byte[] data = getBinaryFromSomeWhere(request.getParameter("xyz"));
response.setHeader("Content-length", Integer.toString(data.length));
response.setHeader("Content-Disposition", "attachment; filename=xyz.bin");
response.getOutputStream().write(data, 0, data.length);
response.getOutputStream().flush();
%>
Mike
  • 2,417
  • 1
  • 24
  • 33
akarnokd
  • 69,132
  • 14
  • 157
  • 192
  • `contentType`. `content-type` fails on Liferay 6. Stack Overflow won't let me suggest single char edits. – Cees Timmerman Oct 22 '12 at 08:33
  • `java.io.Writer` has `write(..)` methods taking `char[]` as an argument but no methods for `byte[]` (at least in Java 7). – Andre Holzner Oct 02 '14 at 12:58
  • Prefer instead: <%@ page contentType="applicaton/octet-stream" %><% byte[] data = getBinaryFromSomeWhere(request.getParameter("xyz")); response.setHeader("Content-length", Integer.toString(data.length)); response.setHeader("Content-Disposition", "attachment; filename=xyz.bin"); response.getOutputStream().write(data, 0, data.length); response.getOutputStream().flush(); %> – Mike May 14 '15 at 11:34