views:

289

answers:

4

This may sound totally stupid, but is a case of real life :(

I'm able to display a HTML table with a "virtual" link name.

Something like this:

Xyz description   <a href="docId=123">document.doc</a>

Xyz description  <a href="docId=456">documentB.doc</a>

Xyz description  <a href="docId=798">documentC.doc</a>

This doc id represents an id in the database ( for these docs are stored in a blob as byte[] )

Anyway. I'm able to get that id, query the database and retrieve the byte[] ( and even store it in a tmp file )

What I can't figure out how to do, is, that when the user click on the link ( and after I perform the db retrieval ) "serve" the byte[] to the user.

Now the very worst part, and what makes me ask this question here is, I need to do this with JSP only ( no servlet ) and without 3rd party libraries ( just... don't ask me why I hate it too )

So. How do I serve in a jsp the binary content of a byte array stored in the server file system

My first guest is:

<%
InputStream read // read the file form the fle system 
response.getOutputStream().write( theBytesReader );
%>

Am I close to the solution?

Would this work to the client as if he had clicked really in the server for a real file?

Thanks in advance.

A: 

Something like this...

InputStream instr = null;
try {
    instr = new BufferedInputStream( new FileInputStream("file.txt") );
    for(int x=instr.read(); x!=-1; x=instr.read()){
        out.write(x);
    }
} finally {
    out.close();
    if( instr != null) instr.close();
}

You will need this as the response to the click (either on a page reload or in another jsp file).

There are better buffering solutions you can do with the write using byte arrays rather than one at a time... I will leave that for you.

Sorry you are stuck in JSP scriptlet land...Hope this helps.

cjstehno
You need to set the MIME type in the response as well like in my example.
Taylor Leese
True... I was assuming that part, thanks for clarifying though.
cjstehno
A: 

You need to set the MIME type in the HTTP response like below in addition to the sample code you provided.

response.setContentType("application/octet-stream");

Note, the application/octet-stream MIME type is used to indicate a binary file.

Taylor Leese
Yea, I have that already, actually I'm using like: `application/vnd.ms-excel` but my real question is, would this method work? ( reading and writing using response.out? )
OscarRyz
Yes, it should as long as the MIME type is correct.
Taylor Leese
Actually, people do this fairly often to serve dynamic images where you have an image URL that is something like /foo/bar/yourImage.jsp and it serves the image dynamically by returning the bytes of the image as well as setting image/gif or similar as the content-type.
Taylor Leese
+3  A: 

To the point, just write the same code in JSP as you would do in a Servlet class. You can practically copypaste it. Only ensure that you are not writinig any template text to the stream, this includes linebreaks and whitespace outside the scriptlets. Otherwise it would get written to the binary file as well and corrupt it.

If you have multiple scriptlet blocks, then you need to arrange them so that there's no linebreak between the ending %> of a scriptlet and the starting <% of the next scriptlet. Thus, e.g.

<%@page import="java.io.InputStream" %><%
    //...
%>

instead of

<%@page import="java.io.InputStream" %>
<%
    //...
%>
BalusC
A: 

Please, please, please don't do this.

You're doing a disservice to your users.

HTTP is amazingly rich in terms of what it can do with files. Caching, chunking, random access, etc.

Take a look at something like FileServlet, and hammer that to fit. Yes, it's a Servlet, rather than a JSP, but this is what you want to do to be a good HTTP citizen.

Some containers have other options you can use, you can hack Tomcats DefaultServlet, etc.

Will Hartung
This is not an answer to my question but a comment, I'm aware this could be achieved with servlets, but if you read my question this is not an option :( I'm sorry but your answer is not helpful
OscarRyz
A JSP is a servlet, you can pretty much embed 99% of the code in to your JSP and get the same thing.
Will Hartung