Here you can find the source of copyStreamToFile(InputStream inputStream, File destFile)
public static void copyStreamToFile(InputStream inputStream, File destFile) throws IOException
//package com.java2s; /******************************************************************************* * Copyright (c) 2014 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * and Eclipse Distribution License v1.0 which accompany this distribution. * The Eclipse Public License is available at * http://www.eclipse.org/legal/epl-v10.html * and the Eclipse Distribution License is available at * http://www.eclipse.org/org/documents/edl-v10.php. * * Contributors://from w ww. j a v a 2 s. c o m * Jochen Hiller *******************************************************************************/ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { public static void copyStreamToFile(InputStream inputStream, File destFile) throws IOException { // ensure that parent dirs are created File parentDir = destFile.getParentFile(); if (parentDir != null) { parentDir.mkdirs(); } FileOutputStream fos = new FileOutputStream(destFile); copyStream(inputStream, fos); fos.close(); } public static void copyStream(InputStream inputStream, OutputStream outputStream) throws IOException { byte[] bytes = new byte[4096]; int read = inputStream.read(bytes, 0, 4096); while (read > 0) { outputStream.write(bytes, 0, read); read = inputStream.read(bytes, 0, 4096); } } }