Here you can find the source of writeFileFromStream(File tempFile, InputStream in)
public static int writeFileFromStream(File tempFile, InputStream in) throws IOException
//package com.java2s; /****************************************************************************** * Copyright (c) 2010 Oracle//from w w w . j a v a 2s . c o m * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Original file was copied from * org.eclipse.wst.common.project.facet.core.util.internal.FileUtil * ******************************************************************************/ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; public class Main { public static int writeFileFromStream(File tempFile, InputStream in) throws IOException { byte[] buffer = new byte[1024]; BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tempFile)); BufferedInputStream bin = new BufferedInputStream(in); int bytesRead = 0; int bytesTotal = 0; // Keep reading from the file while there is any content // when the end of the stream has been reached, -1 is returned while ((bytesRead = bin.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); bytesTotal += bytesRead; } if (bin != null) { bin.close(); } if (out != null) { out.flush(); out.close(); } return bytesTotal; } }