aspose file tools*
The moose likes Web Services and the fly likes ServletOutputStream truncated only when invoked remotely Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "ServletOutputStream truncated only when invoked remotely" Watch "ServletOutputStream truncated only when invoked remotely" New topic
Author

ServletOutputStream truncated only when invoked remotely

Zsolt Nagykaldi
Greenhorn

Joined: Jun 05, 2008
Posts: 7

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!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
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

Joined: Jun 05, 2008
Posts: 7

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

Zsolt
Zsolt Nagykaldi
Greenhorn

Joined: Jun 05, 2008
Posts: 7

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

Joined: Jun 05, 2008
Posts: 7

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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ServletOutputStream truncated only when invoked remotely