Java InputStream to File InputStream2File(InputStream in, String filePath)

Here you can find the source of InputStream2File(InputStream in, String filePath)

Description

Input Stream File

License

Open Source License

Declaration

public static String InputStream2File(InputStream in, String filePath) 

Method Source Code


//package com.java2s;

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

public class Main {
    public static String InputStream2File(InputStream in, String filePath) {
        FileOutputStream fos = null;
        String fileName = filePath + File.separator + System.currentTimeMillis() + ".jpg";
        try {//  ww  w .  j a  va2s.  co  m
            int flag = 0;
            fos = new FileOutputStream(fileName);
            byte[] b = new byte[1024];
            while ((flag = in.read(b, 0, 1024)) != -1) {
                fos.write(b, 0, flag);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                in.close();
                fos.close();
            } catch (IOException e) {

                // TODO Auto-generated catch block
                e.printStackTrace();

            }
        }

        return fileName;
    }
}

Related

  1. inputStream2File(InputStream in, File file)
  2. InputStreamToFile(final InputStream stream, final File file)
  3. inputStreamToFile(InputStream byteStream, String pathFileName)
  4. inputStreamToFile(InputStream in, File file)
  5. inputStreamToFile(InputStream ins, File file)