I have a page in JSP that list some file that could be downloaded by a user. Thoses files are not on the local server, they are on a remote file server.
When the use
If forced to use jsp (and not a servlet), you may take a look at this how-to
It uses the ServletOutputStream
, which is more appropriate for binary content, rather than a JspWriter
.
Also note the settings for trimming the whitespaces.
JSP is a view technology. Everything outside the scriptlets <% %>
will be printed to the response, including whitespace characters such as newlines. It would surely corrupt binary files.
You could trim the whitespace in the JSP file, but scriptlets are discouraged since a decade and nowadays considered bad practice. Raw Java code belongs in Java classes, not in JSP files. The real solution is to use a Servlet
for this.
Create a class which extends HttpServlet
, implement the doGet()
method, move the Java code from the JSP file into this method, map this servlet on a certain url-pattern
and your problem should disappear. You can find here a basic example of such a servlet.
Apart from this problem is that you're storing the entire file in a byte[]
instead in an InputStream
. I am not sure what your ClientTCPStockage
actually is doing, but I'd suggest to fix that as well. This because every byte
of a byte[]
costs effectively one byte of JVM's memory. Imagine that you have 128MB of JVM memory and that there are more than 10 users concurrently running this piece code with a file of larger than 12.8MB. Yes, OutOfMemoryError
.