Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

java - JSP download - application/octet-stream

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...