Here you can find the source of writerFromInputStream(InputStream stream, String path, String fileName)
public static void writerFromInputStream(InputStream stream, String path, String fileName) throws IOException
import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; public class Main{ private static Logger logger = Logger.getLogger(FileUtil.class); /*from w ww . java 2 s. co m*/ public static void writerFromInputStream(InputStream stream, String path, String fileName) throws IOException { FileOutputStream fs = null; try { File file = new File(path); if (!file.exists()) { file.mkdirs(); } fs = new FileOutputStream(path + File.separator + fileName); byte[] buffer = new byte[1024 * 1024]; int byteread = 0; while ((byteread = stream.read(buffer)) != -1) { fs.write(buffer, 0, byteread); fs.flush(); } } catch (Exception e) { logger.error("Write stream error!", e); } finally { if (fs != null) { try { fs.close(); } catch (Exception e) { logger.warn("Resource FileOutputStream error!", e); } } if (stream != null) { try { stream.close(); } catch (Exception e) { logger.warn("Resource InputStream error!", e); } } } } }