File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Problem With Download File Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem With Download File Servlet" Watch "Problem With Download File Servlet" New topic
Author

Problem With Download File Servlet

Kerry Baer
Ranch Hand

Joined: Jun 23, 2010
Posts: 39
I've put together a Servlet called DownloadFile that takes in an id for a database record that contains the location of the file I want to load. It's supposed to grab the file and provide the end-user with the option to open or save the file (whatever behavior their browser decides).

When I go into the directory where the files are held, I can open all of the files with the appropriate programs and there aren't any problems with the files.

When I run my web app and try to open or save-then-open the file using the DownloadFile Servlet, I get an error. For example: I've got a PDF file. Using windows explorer and navigating to the document, I can open and view the document in Adobe Acrobat. When I view the file using the DownloadFile Servlet, I get the following error message in Adobe Acrobat: "There was an error opening this document. The file is damaged and could not be repaired."

I will appreciate any help figuring out what I am doing wrong.

Here is my DownloadFile Servlet code:
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618

You always write 4096() bytes. On the last read(), could it read less then 4096 bytes, thereby writing garbage at the end of the file?
Kerry Baer
Ranch Hand

Joined: Jun 23, 2010
Posts: 39
It appears to be only PDF files that do not open properly using this Servlet. I've tested and verified that it can open JPG, PNG, DOC, DOCX, XLS, XLSX, GIF, and TXT files.

There must be something about doing this with PDF files that causes the problem. Any ideas?
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Kolo Bear wrote:It appears to be only PDF files that do not open properly using this Servlet. I've tested and verified that it can open JPG, PNG, DOC, DOCX, XLS, XLSX, GIF, and TXT files.

There must be something about doing this with PDF files that causes the problem. Any ideas?

Did you try my suggestion and find that it still produces the erroneous results?
Kerry Baer
Ranch Hand

Joined: Jun 23, 2010
Posts: 39
Tom Reilly wrote:
You always write 4096() bytes. On the last read(), could it read less then 4096 bytes, thereby writing garbage at the end of the file?


I got this part of my code from the web, so I'm not sure how to determine if the last read is less than 4096 bytes.
Kerry Baer
Ranch Hand

Joined: Jun 23, 2010
Posts: 39
If figured it out. Tom, you were probably right.

After I created the File object, I added the line:


This corrected the problem and the files are now opening as expected. I assume this fixes the "bad bytes" issue you were trying to explain to me.

Thank you for your help!
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
It's great that your solution worked. FYI, the fileIn.read(outputByte, 0, 4096) returns the number of bytes read. So you could have put this number in a variable and used it in the write() method instead of hard-coding 4096.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem With Download File Servlet
 
Similar Threads
web service integration using Netbeans IDE
not able to insert values in ms access database
Httpsesion Listener is not working properly?
sending email in servlet
HTTP Status 404 - Servlet is not available