如何在Java中将InputStream转换为File

时间:2025-03-28 08:42:37

以下是一些将InputStream转换为File Java示例

  1. 手动将InputStream复制到FileOutputStream
  2. Apache Commons IO –
  3. Java 1.7 NIO

1. FileOutputStream

1.1我们必须将数据从InputStream手动复制到OutputStream

package ;

import ;
import ;
import ;
import ;
import ;

public class InputStreamToFile {

    private static final String FILE_TO = "d:\\download\\";

    public static void main(String[] args) throws IOException {

        URI u = ("/");
        try (InputStream inputStream = ().openStream()) {
            File file = new File(FILE_TO);
            copyInputStreamToFile(inputStream, file);
        }
    }

    // InputStream -> File
    private static void copyInputStreamToFile(InputStream inputStream, File file) 
		throws IOException {

        try (FileOutputStream outputStream = new FileOutputStream(file)) {

            int read;
            byte[] bytes = new byte[1024];

            while ((read = (bytes)) != -1) {
                (bytes, 0, read);
            }

			// commons-io
            //(inputStream, outputStream);

        }

    }

}

2. Apache Commons IO

2.1 在Apache Commons IO中可用

<dependency>
		<groupId>commons-io</groupId>
		<artifactId>commons-io</artifactId>
		<version>2.6</version>
	</dependency>
package ;

import ;

import ;
import ;
import ;
import ;

public class InputStreamToFile2 {

    private static final String FILE_TO = "d:\\download\\";

    public static void main(String[] args) throws IOException {

        URI u = ("/");
        try (InputStream inputStream = ().openStream()) {

            File file = new File(FILE_TO);

			// commons-io
            (inputStream, file);
        }

    }

}

3. Java 1.7 NIO

3.1如果只想将inputStream保存到某个文件中,请尝试使用Java 1.7 NIO

package ;

import ;
import ;
import ;
import ;
import ;

public class InputStreamToFile3 {

    private static final String FILE_TO = "d:\\download\\";

    public static void main(String[] args) throws IOException {

        URI u = ("/");
        try (InputStream inputStream = ().openStream()) {

			//Java 1.7
            (inputStream, (FILE_TO));

        }

    }

}

4.旧时光

4.1在过去的Java 1.7之前,我们必须手动关闭所有资源。

package ;

import .*;

public class InputStreamToFile4 {

    public static void main(String[] args) {

        InputStream inputStream = null;
        OutputStream outputStream = null;

        try {
            // read this file into InputStream
            inputStream = new FileInputStream("/Users/mkyong/");

            // write the inputStream to a FileOutputStream
            outputStream = new FileOutputStream(new File("/Users/mkyong/"));

            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = (bytes)) != -1) {
                (bytes, 0, read);
            }

            ("Done!");

        } catch (IOException e) {
            ();
        } finally {
            if (inputStream != null) {
                try {
                    ();
                } catch (IOException e) {
                    ();
                }
            }
            if (outputStream != null) {
                try {
                    ();
                } catch (IOException e) {
                    ();
                }

            }
        }
    }

}

5.将文件转换为InputStream

这很容易:

File file = new File("d:\\download\\");
    InputStream inputStream = new FileInputStream(file);

注意
您可能对此String的InputStream感兴趣

参考文献

  • 文件JavaDocs
  • 如何在Java中将InputStream转换为String
  • Apache Commons IO

翻译自: /java/how-to-convert-inputstream-to-file-in-java/