• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem With Download File Servlet

 
Kerry Baer
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic