Download file from server using jsp....

Posted on 2003-03-13
Medium Priority
Last Modified: 2010-03-19
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
     String separator = System.getProperty("file.separator");
     String rootPath = System.getProperty("user.dir")+separator;
     File file = new File(rootPath+"test.doc");
        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){

Plz help me out...

Question by:hcwang
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 15

Expert Comment

ID: 8134465
How not right is it?


Expert Comment

ID: 8134514
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 -
LVL 15

Accepted Solution

WalkaboutTigger earned 750 total points
ID: 8138056
dansoto, setting the MIME type to application/unknown tells (or is supposed to tell) the client application to use a binary file transfer.

Limited time offer using promo code EXPERTS30

Designed with a wealth of functionality and convenience, ATEN's new Thunderbolt™ 2 Sharing Switch takes your Thunderbolt setup to the next level. Now through September 15, 2017, Experts Exchange members get 30% off the US7220 on the ATEN USA eShop using promo code EXPERTS30.


Expert Comment

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

- dan soto -

Author Comment

ID: 8141961
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.


Expert Comment

ID: 9153113
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 
Post your closing recommendations!  No comment means you don't care.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
For many of us, the  holiday season kindles the natural urge to give back to our friends, family members and communities. While it's easy for friends to notice the impact of such deeds, understanding the contributions of businesses and enterprises i…
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question