Java : Write file contents to OutputStream

This page last changed on Feb 12, 2007 by Kees de Kooter

Ugly code alert

 

OutputStream outputStream = response.getOutputStream();
    
FileInputStream fileInputStream = new FileInputStream(file);
            
// Copy the contents of the file to the output stream
byte[] buffer = new byte[1024];
int count = 0;
                
while ((count = fileInputStream.read(buffer)) >= 0) {
    outputStream.write(buffer, 0, count);
}                

outputStream.flush();