Java InputStream to File writeStreamToFile(InputStream inputStream, String extension)

Here you can find the source of writeStreamToFile(InputStream inputStream, String extension)

Description

write Stream To File

License

Open Source License

Declaration

public static File writeStreamToFile(InputStream inputStream, String extension) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.UUID;

public class Main {
    public static File writeStreamToFile(InputStream inputStream, String extension) throws IOException {
        File file = File.createTempFile("file" + UUID.randomUUID(), "." + extension);
        return writeStreamToFile(inputStream, extension, file);
    }/*from  w w  w . jav a2 s  .  com*/

    public static File writeStreamToFile(InputStream inputStream, String extension, File file) throws IOException {
        FileOutputStream output = new FileOutputStream(file);

        byte buf[] = new byte[1024];
        int len;
        while ((len = inputStream.read(buf)) > 0) {
            output.write(buf, 0, len);
        }
        output.close();
        inputStream.close();

        return file;
    }
}

Related

  1. writeStreamToFile(InputStream _actual, File _file)
  2. writeStreamToFile(InputStream in, OutputStream out)
  3. writeStreamToFile(InputStream in, String fileName)
  4. writeStreamToFile(InputStream inputStream, File file)
  5. writeStreamToFile(InputStream inputStream, File targetFile)
  6. writeStreamToFile(InputStream is, File file)
  7. writeStreamToFile(InputStream is, File out)
  8. writeStreamToFile(InputStream is, File toFile)
  9. writeStreamToFile(InputStream is, String fileName)