Here you can find the source of saveFile(File file, InputStream is)
public static void saveFile(File file, InputStream is)
//package com.java2s; /*//w w w. ja v a 2s. c om * Copyright 2010 Torkjel Hongve (torkjelh@conduct.no) * * This file is part of Upside. * * Upside is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Upside is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Upside. If not, see <http://www.gnu.org/licenses/>. */ import java.io.BufferedOutputStream; 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 saveFile(File file, InputStream is) { OutputStream os; try { os = new BufferedOutputStream(new FileOutputStream(file)); } catch (IOException e) { throw new RuntimeException(e); } pipe(is, os); } public static void pipe(InputStream in, OutputStream out) { byte[] data = new byte[4096]; try { int len; while ((len = in.read(data)) != -1) out.write(data, 0, len); out.flush(); } catch (IOException e) { throw new RuntimeException(e); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } }