• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ServletOutputStream truncated only when invoked remotely

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have been struggling with this weird problem. I pull files from a database saved in binary form (e.g. pdf, images, docs, etc) and stream them to the client's browser using ServletOutputStream. It works like a charm when I test it in a Tomcat standalone with Java Struts from the localhost. No problem. However, files larger than about 60K are truncated when they are streamed back from a remote production server that has the exact same setup as my test environment locally. I tried messing with buffers, flushing, closing, even padding the end of the files. All work well from localhost, but files get truncated (resulting in corrupted files) from the online server. I checked and the files are saved correctly in the database (they have the right length), but when they are streamed, a chunk is chopped off the end. If I use a hex editor to "glue" the chopped bytes back to the end of the corrupted files, they open fine and they are repaired. I suspect some sort of asynchronous action in buffering and sending bytes over the net, but I don't know how to fix it. I would appreciate your help or any good idea! The part of my code where I send the data to the ServletOutputStream is listed below.



Thanks!
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very mysterious alright.

If this was my problem I would drop the statement that sets the response buffer size and let it run with the default.

Bill
 
Zsolt Nagykaldi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Good idea, but in fact, I tried to set the buffer because the file was corrupted without it.

Zsolt
 
Zsolt Nagykaldi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

New info:

I found that when I browse from the web server directly, files are still truncated. I did this via domain name resolution, not localhost loopback (which would require stopping the server and reconfiguring). Besides distance from the server the other possibility could be using SuSe Linux Enterprise 10 which runs Tomcat and Java on the server, while the test box, where I see no problem is a Win XP environment. Could it be Tomcat environment settings? How about Java settings?
 
Zsolt Nagykaldi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

OK, I got it. For some reason, when I write files to the ServletOutputStream as a ByteArrayOutputStream, it does not work from a remote server (truncates the file). However if I write a bytearray directly to the SOS, it works perfectly in all environments.

This could be a good solution for others with the same problem.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then I expect that means that some part of your code which you didn't show is causing the problem. I don't understand why you need a ByteArrayOutputStream to copy data from a file to the servlet's output stream, but my guess is that you didn't fill the ByteArrayOutputStream correctly.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic