Here you can find the source of copyFileFromStream(InputStream in, File dest)
public static void copyFileFromStream(InputStream in, File dest) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.BufferedInputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { public static void copyFileFromStream(InputStream in, File dest) throws IOException { try {//from www. j a v a 2 s.c o m if (!dest.exists()) { dest.getParentFile().mkdirs(); dest.createNewFile(); } } catch (IOException e) { throw e; } OutputStream out = null; BufferedInputStream bin = new BufferedInputStream(in); byte[] buffer = new byte[4096]; int bytesRead = 0; try { out = new FileOutputStream(dest); } catch (FileNotFoundException e1) { throw new RuntimeException("Impossible! File not found?!", e1); } try { while ((bytesRead = bin.read(buffer)) > 0) { out.write(buffer, 0, bytesRead); } } catch (IOException e) { throw e; } finally { try { bin.close(); } catch (IOException e) { throw new RuntimeException("Cannot close input stream!", e); } try { out.close(); } catch (IOException e) { throw new RuntimeException("Cannot close output stream!", e); } } } }