Search code examples
javaperformancefileservletsnio

Most effective way to write File to ServletOutputStream


ServletOutputStream output = response.getOutputStream();
output.write(byte[]);

What is the most effective way to write File to javax.servlet.ServletOutputStream?

EDIT:

won't this be more effective if the NIO was used?


Solution

  • IOUtils.copy(in, out);
    out.flush();
    //...........
    out.close(); // depends on your application
    

    Where in is the FileInputStream and out is the SocketOutputStream. IOUtils is a utility from Commons IO module in Apache Commons.