tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tim Funk <funk...@joedog.org>
Subject Re: Show files on filesystem
Date Wed, 16 Apr 2003 13:52:03 GMT
Actually that was a little inefficientbut was OK. You are not streaming the 
file as it is read. For efficiency, use a byte buffer to reduce the number of 
function calls.

  File f = new File(valueObj.getPath());
  InputStream is = new BufferedInputStream(new
  FileInputStream(valueObj.getPath()));
  OutputStream os = response.getOutputStream();
  response.setContentType("application/pdf");

  byte[] buffer = new byte[1024]; /*or whatever size*/

  int read = is.read(buffer);
  while (read >=0 ) {
     if (read>0)
         os.write(buffer,0, read);
     read = is.read(buffer);
  }

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

-Tim

harm@informatiefabriek.nl wrote:
> Hehe ;-)
> 
> I know how java IO works... 
> I have coded the following:
> 
> -----------------------
> File f = new File(valueObj.getPath());
> InputStream is = new BufferedInputStream(new 
> FileInputStream(valueObj.getPath()));
> OutputStream os = response.getOutputStream();
> response.setContentType("application/pdf");
> int b;
> while ((b = is.read()) != -1) {
>         os.write(b);
> }
> os.flush();
> os.close();
> is.close(); 
> ----------------------------
> 
> I'm just wondering if this method is efficient. Because the file first has 
> to be read completely and then written to the output stream.
> 
> Thanks,
> 
> Harm.
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message