Java InputStream to OutputStream copyStreamToFile(InputStream inputStream, File outputFile)

Here you can find the source of copyStreamToFile(InputStream inputStream, File outputFile)

Description

copy Stream To File

License

Open Source License

Declaration

public static File copyStreamToFile(InputStream inputStream, File outputFile) throws Exception 

Method Source Code

//package com.java2s;
/**/*from  w  ww . j  a v a2 s . c om*/
 * License: https://github.com/votingsystem/votingsystem/wiki/Licencia
 */

import java.io.*;

public class Main {
    private static final int BUFFER_SIZE = 4096;

    public static File copyStreamToFile(InputStream inputStream, File outputFile) throws Exception {
        OutputStream outputStream = new FileOutputStream(outputFile);
        byte[] buf = new byte[BUFFER_SIZE];
        int len;
        while ((len = inputStream.read(buf)) > 0) {
            outputStream.write(buf, 0, len);
        }
        outputStream.close();
        inputStream.close();
        return outputFile;
    }

    public static File copyStreamToFile(InputStream inputStream) throws Exception {
        File outputFile = File.createTempFile("streamToFile", ".html");
        outputFile.deleteOnExit();
        OutputStream outputStream = new FileOutputStream(outputFile);
        byte[] buf = new byte[BUFFER_SIZE];
        int len;
        while ((len = inputStream.read(buf)) > 0) {
            outputStream.write(buf, 0, len);
        }
        outputStream.close();
        inputStream.close();
        return outputFile;
    }
}

Related

  1. copyStreams(InputStream source, OutputStream target)
  2. copyStreamSafely(InputStream in, ByteArrayOutputStream os)
  3. copyStreamToFile(final InputStream stream, final File output)
  4. copyStreamToFile(InputStream input, String outputPath)
  5. CopyStreamToFile(InputStream inputStream, File outputFile)
  6. copyStreamToFile(InputStream is, File outputFile)
  7. copyStreamToFile(InputStream stream, String outputFilePath)
  8. copyStreamToFileBinary(final InputStream stream, final File output)
  9. copyStreamToStream(final InputStream input, final OutputStream output)