Download file from server using jsp....

Dear experts,

  I encoutered a problem that the size and format of the file download from server using JSP is not correct. While I open the file downloaded from server the content of the file is not right.
Here is my JSP code.
--------------------------------------------------------------------------------------------------------
<%@ page
     import="java.io.*,java.util.*,java.text.*,java.net.*"    
%>
<%
         
     String separator = System.getProperty("file.separator");
     
     String rootPath = System.getProperty("user.dir")+separator;
             
     File file = new File(rootPath+"test.doc");
     
     response.setContentType("application/unknown");    
       
        response.setHeader("Content-Disposition", "attachment; filename=test.doc;");
         
        ServletOutputStream sos = response.getOutputStream();
       
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
       
        int data;
     while((data = bis.read())!=-1){
         sos.write(data);          
     }
     
     bis.close();
     sos.close();                
%>
--------------------------------------------------------------------------------------------------------

Plz help me out...

Regards,
hcwangAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
WalkaboutTiggerConnect With a Mentor Commented:
dansoto, setting the MIME type to application/unknown tells (or is supposed to tell) the client application to use a binary file transfer.

Walkabout
0
 
WalkaboutTiggerCommented:
How not right is it?

Walkabout
0
 
dansotoCommented:
Not familiar with this but maybe it is similar to FTP where you have to use the "bin" command to specify a file type of binary as opossed to asci.

dunno for sure...


- ds -
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
dansotoCommented:
cool..thanks.  Like I said I didn't really know the answer but was just shooting ideas :-)

- dan soto -
0
 
hcwangAuthor Commented:
Dear experts,

  I found where the problem is. I add a line "response.setContentLenth(file.size())" in the JSP and the problem is solved. The size of the file downloaded from server is correct.

Regards,
0
 
CleanupPingCommented:
hcwang:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
All Courses

From novice to tech pro — start learning today.