0

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 user click to download a file, the webserver connect via TCP to the file server. The web server download the file and create a HTTP response for the client.

Here is my code:

<%@page language="java"%>
<%@page import="sun.misc.Request"%>
<%@page import="listing.ClientTCPStockage"%>
<%@page import="java.net.InetAddress"%>

<%
out.clearBuffer();

String nomFichier = request.getParameter("fichier");
String adresseStockage = request.getParameter("adresseStockage");

ClientTCPStockage clientStockage = new ClientTCPStockage(InetAddress.getByName(adresseStockage), 2004);
byte donneeFichier[] = clientStockage.getDonneesFichier(nomFichier);

response.setHeader("Content-Disposition", "attachment;filename=\"" + nomFichier + "\"");
response.setHeader("Content-Type", "application/octet-stream;");
response.setHeader("Accept-Ranges", "bytes");
response.setHeader("Content-Length", String.valueOf(donneeFichier.length));

for(int i = 0; i < donneeFichier.length; i++){
    out.write(donneeFichier[i]);
}
%>

This is working perfectly fine for text-based file, like .csv or normal .txt but It doesnt work for other type like .mp3 or .jpeg.. the files end up corrupt.

I think there is a problem with my encoding but I can't find where..

Here is the HTTP Header response:

HTTP/1.x 200 OK
Server: Apache-Coyote/1.1
Content-Disposition: attachment;filename="test.mp3"
Accept-Ranges: bytes
Content-Type: application/octet-stream;
Content-Length: 5387668
Date: Sun, 20 Dec 2009 18:52:18 GMT

Thanks.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Hubert Perron
  • 3,022
  • 5
  • 34
  • 28
  • ClientTCPStockage connect to the file server and grab the file as a byteArray. At this point the content is valid because when I tries to put it back on the hard drive as a file it works perfectly. Data get corrupt when I output it as a HTTP response – Hubert Perron Dec 20 '09 at 19:07
  • Probably a dup: see http://stackoverflow.com/questions/1776142/getoutputstream-has-already-been-called-for-this-response/1776161#1776161 for the perils of writing to an `OutputStream` from a JSP. – skaffman Dec 20 '09 at 20:14

2 Answers2

4

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • +1 I didn't catch the whitespace. Although I agree that the Servlet would be cleaner and the best practice, let's at least mention that the OP could simply squish the first 6 lines together like this `<%@page language="java"%><%@page import="sun.misc.Request"%><%@page import="listing.ClientTCPStockage"%><%@page import="java.net.InetAddress"%><%` and the jsp page would probably work as is. Some days you've just gotta get things done without having to refactor all your jsps into Servlets. :) – Asaph Dec 20 '09 at 19:28
  • Also, be sure there is no trailing whitespace at the end of the file after the `%>`. – Asaph Dec 20 '09 at 19:30
  • white space should not be an issue because I call out.clearBuffer() at the start – Hubert Perron Dec 20 '09 at 19:34
  • I would like to use servlet but I can't, this is a school project, and I have to use classic JSP. I know I should use MVC, servlet, grails... But I can't ! – Hubert Perron Dec 20 '09 at 19:37
  • @Hubert Perron: `clearBuffer()` won't clear anything that's already been flushed to the client. And it won't throw any Exception either (`clear()` will). Squishing lines 1-6 together is at least worth a try. Let us know if it works. – Asaph Dec 20 '09 at 19:50
  • Get rid of **any** whitespace outside the `<% %>` things (spaces, newlines, etc) and also ensure that your JSP isn't saved with a BOM. – BalusC Dec 20 '09 at 19:56
  • @Asaph Just tried it and the files are still corrupt. When I look at the file with an editor some char seem to be replaced.. Normal source file: PØ+«¸š6Ñ$u$¦•1å§YI*ÅÌf±â‘å7tê1õ‚^o After HTTP download: P?+???6?$u$??1??YI*??f????7t?1??^o – Hubert Perron Dec 20 '09 at 19:58
  • @BalusC I'm sure the is no whitespace/newlines lefts. How can I check for the BOM ? – Hubert Perron Dec 20 '09 at 19:59
  • @Hubert: Your binary files are been read or written as characters. Use InputStream/OutputStream instead of Reader/Writer. – BalusC Dec 20 '09 at 20:04
4

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.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140