servlet problem with file transfer

Hi !

----

The following code should copy a file on disk to the requesting client,
but the downloaded
file differs in size and content from the original file.
Any hint whats going wrong here ?

-- snip --
    private void putFile( DownloadItem di, HttpServletResponse res )
throws IOException
    {
        d( "sending '" + di.filename + "' (" + di.contenttype + ")" );

        // get source
        FileInputStream in = new FileInputStream( di.filename );

        // prepare and get destination
        res.setContentType(   di.contenttype   );
        res.setContentLength( (int)di.filesize ); /* seems that here
sits a possible problem for files >= 2 GB... */
        OutputStream out = res.getOutputStream();

        int  readlen,
             sentsize = 0;
        byte buffer[] = new byte[ 256 ];

        // loop until EOF
        while( (readlen = in.read( buffer )) != -1 )
        {
          // throws IOException: broken pipe when download is canceled.
          out.write( buffer, 0, readlen );
          sentsize += readlen;
        }

        // Success ! Close streams.
        out.close();
        in.close();

        d( "transfer of " + di.filename + " (" + di.filesize + "/" +
sentsize + ") done." );
    }
-- snip --

----

Bye,
Roland

--
  __ .  . __
 (o.\ \/ /.o)  Roland Mainz                               C programmer
  \__\/\/__/   Roland.Mainz@informatik.med.uni-giessen.de MPEG specialist
  /O /==\ O\   gisburn@w-specht.rhein-ruhr.de             Sun&&Amiga programmer
 (;O/ \/ \O;)  TEL +49 (0) 2426901568  FAX +49 (0) 2426901569

Received on Thursday, 1 April 1999 09:31:36 UTC