Here you can find the source of copyFileToStream(File input, OutputStream os)
public static void copyFileToStream(File input, OutputStream os) throws IOException
//package com.java2s; //License from project: Apache License import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { public static void copyFileToStream(File input, OutputStream os) throws IOException { InputStream is = new BufferedInputStream(new FileInputStream(input)); copyStreamToStream(is, os);//from w w w.java 2 s . c o m is.close(); } /** * Copy contents of an input stream to an output stream. Leave both * streams open afterwards. * @param is input stream * @param os output stream * @throws IOException */ public static void copyStreamToStream(InputStream is, OutputStream os) throws IOException { byte[] buffer = new byte[4096]; for (int read = is.read(buffer); read != -1; read = is.read(buffer)) { os.write(buffer, 0, read); } os.flush(); } }