tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject Re: Stream PDF using servlet
Date Thu, 24 Apr 2003 15:13:29 GMT
I have the same problem....

I use this code:

InputStream is =
        new BufferedInputStream(
                new FileInputStream(valueObj.getPath()));
File f = new File(valueObj.getPath());
OutputStream os = response.getOutputStream();
//      Report the type and size of content being sent back.
response.setContentLength((int) f.length());
response.setHeader("Cache-Control", "no-cache");
                          "attachment; filename=" +
byte[] buffer = new byte[1024]; /*or whatever size*/
int read =;
while (read >= 0) {
        if (read > 0)
                os.write(buffer, 0, read);
        read =;

/* NOT NEEDED  os.flush(); */

There is a major difference between browsers and the OS the user uses.
But I found that this code works with IE and Mozilla on windows.
I also works on MacOS X using IE 5.0 to 5.2....

You might give it a try!

Just for my personal interest... How does an http-server like apache 
handle these request.
Because when I use WGet to see the headers (on linux) it outputs the 

[root@aphrodite root]# wget -S
           => `webjsrv.pdf.1'
Resolving done.
Connecting to[]:80... connected.
HTTP request sent, awaiting response...
 1 HTTP/1.1 200 OK
 2 Date: Thu, 24 Apr 2003 15:02:03 GMT
 3 Server: Apache/1.3.23 (Unix)  (Red-Hat/Linux) mod_python/2.7.6 
Python/1.5.2 PHP/4.1.2 mod_perl/1.24_01 FrontPage/
 4 Last-Modified: Wed, 04 Sep 2002 21:17:26 GMT
 5 ETag: "f5a1f-c966a-3d767866"
 6 Accept-Ranges: bytes
 7 Content-Length: 824938
 8 Connection: close
 9 Content-Type: application/pdf

So what is so hard about doing it in Java?


Harm de Laat

Lior Shliechkorn <> 
04/24/2003 04:46 PM
Please respond to
"Tomcat Users List" <>

Tomcat Users List <>

Re: Stream PDF using servlet

Lucky for you I just fixed this problem recently. Take out the 
setHeader(..) that have to do with the no-cache and try again.

Mark Lenz <> wrote:
I'm trying to stream a PDF to a browser using a servlet. It works fine
using Mozilla. But, sure enough, I just can't get it to work with IE
(great standards compliance!). When I try to download the PDF through IE
(6.0), it asks me what I want to do (open or save the file). If I choose
open I get an error "Internet Explorer was not able to open this Internet
site. The request site is either unavailable or cannot be found. Please
try again later.". Here's the code from my servlet for streaming the PDF:

File pdf_file = new File("
if (!pdf_file.exists())
//display error message
int filesize = (int)pdf_file.length();
res.setHeader("Pragma", "no-cache");
res.setHeader("Cache-Control", "no-cache");
ServletOutputStream out = res.getOutputStream();

byte[] data = new byte[filesize];
DataInputStream in = new DataInputStream(new

I must be missing a simple thing that IE requires. Anyone have any ideas?

Mark Lenz
Pierce Manufacturing, Inc.
(920) 832-3523

The information contained in this electronic mail message is confidential
information and intended only for the use of the individual or entity 
above, and may be privileged. If the reader of this message is not the
intended recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited. If
you have received this transmission in error, please contact the sender
immediately, delete this material from your computer and destroy all
related paper media. Please note that the documents transmitted are not
intended to be binding until a hard copy has been manually signed by all
Thank you.

To unsubscribe, e-mail:
For additional commands, e-mail:

Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message