Here you can find the source of copyStreamToFile(InputStream inStream, File file)
public static void copyStreamToFile(InputStream inStream, File file) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.channels.Channels; import java.nio.channels.FileChannel; import java.nio.channels.ReadableByteChannel; public class Main { public static void copyStreamToFile(InputStream inStream, File file) throws IOException { try (ReadableByteChannel in = Channels.newChannel(inStream)) { try (FileOutputStream outStream = new FileOutputStream(file)) { FileChannel out = outStream.getChannel(); long offset = 0; long quantum = 1024 * 1024; long count; while ((count = out.transferFrom(in, offset, quantum)) > 0) { offset += count;//from w ww . j av a 2 s . co m } } } } }