Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve a file with JSP?

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.

like image 761
OscarRyz Avatar asked Jan 22 '10 02:01

OscarRyz


People also ask

How do I run a JSP file?

Copy your file to CATALINA_HOME/webapps/ROOT , e.g., c:/Tomcat8/webapps/ROOT . Start the Tomcat server. Start your browser if it is not already running. In the address area of the browser, type http://localhost:8080/DateJSP.jsp and submit that address to the browser.

How do I test a JSP file?

Just create a directory in the tomcat webapps folder and place your JSP file in the newly created directory. For example, if your JSP is located at apache-`tomcat/webapps/test/home. jsp`, then you can access it in browser with URL `https://localhost:8080/test/home.jsp`.

Which option will you use to import a file in JSP?

Use Page Directive to import a Class in JSP page. Page Directive Uses 11 Different types of Attributes , One of them is "import". Page Directive with import Attribute Allows you to Mention more than one package at the same place separated by Commas(,).

How do I save a JSP file?

Navigate to the "File name" text field and type the desired file name within quotes. Add a JSP extension to the file name -- for example, "filename. jsp" -- and click "Save."


2 Answers

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" %>
<%
    //...
%>
like image 82
BalusC Avatar answered Oct 26 '22 23:10

BalusC


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.

like image 32
Taylor Leese Avatar answered Oct 27 '22 00:10

Taylor Leese