tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Anhony" <>
Subject Re: Serving files using tomcat
Date Wed, 04 May 2005 15:02:35 GMT

Take a look at the code fragment below. It should serve as a good starting 
I hope this helps.


    private void processPDFRequest(HttpServletRequest request, 
HttpServletResponse response) throws ServletException, IOException, 
        int bytesCopied = 0;

        FileInputStream fin = null;
        OutputStream out = null;

        String fileAddress = "The fully qualified path to your PDF file";
        if( fileAddress == null )

        int ext = fileAddress.lastIndexOf( '.' );
        if( ext != -1 )
            ext = fileAddress.substring( ext+1, 
fileAddress.length() ).toLowerCase();

            if( ext == "pdf" )
                "Do whatever you think best to do"
            "Do whatever you think best to do"

            out = response.getOutputStream();
            fin = new FileInputStream( fileAddress );
            bytesCopied = StreamCopier.copy( fin, out );
            if( fin != null )
            if( out != null )

----- Original Message ----- 
From: "Steve Vanspall" <>
To: "Tomcat User List" <>
Sent: Wednesday, May 04, 2005 9:29 AM
Subject: Serving files using tomcat


I have been looking around and haven't found a solution that works

basically I have a PDF that gets created dynamically. Now to save memory I 
have the PDF written to a file rather than a ByteArray. The only way I can 
be sure that I wont encounter errors creating the file is to use 
File.createTempFile. The creation goes of ok. And I have checked the file 
itself and the PDF looks great.

How do i now serve this to the user who has requested it. If I try to write 
it to the response (using the same method I use to creare dynamic image, 
this works), it just shows up a blank screen.

The problem also is, even if it did show the PDF, acrobat, to my understand 
will read only chunks of the stream and will go pack to get more. Thisis a 
problem because there is nothing to go back for.

So the point,

If I can just redirect the browser to a file in the tomcat temp directory 
(can I do that, will the use have access to that directory), then how do I 
translate the location of the temp directory to a url that is accesible 

If not then what other suggestions can people give me.

Thanks in advance


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

View raw message