简体   繁体   English

是否可以从 InputStream 创建一个 File 对象

[英]Is it possible to create a File object from InputStream

Is there any way to create a java.io.File object from an java.io.InputStream ?有没有办法从java.io.InputStream创建java.io.File对象?

My requirement is reading the File from a RAR.我的要求是从 RAR 中读取文件。 I am not trying to write a temporary File, I have a file inside RAR archive which I am trying to read.我不是要写一个临时文件,我在 RAR 存档中有一个文件,我正在尝试读取它。

You need to create new file and copy contents from InputStream to that file:您需要创建新文件并将内容从InputStream复制到该文件:

File file = //...
try(OutputStream outputStream = new FileOutputStream(file)){
    IOUtils.copy(inputStream, outputStream);
} catch (FileNotFoundException e) {
    // handle exception here
} catch (IOException e) {
    // handle exception here
}

I am using convenient IOUtils.copy() to avoid manual copying of streams.我正在使用方便IOUtils.copy()来避免手动复制流。 Also it has built-in buffering.它还具有内置缓冲。

In one line:在一行中:

FileUtils.copyInputStreamToFile(inputStream, file);

(org.apache.commons.io) (org.apache.commons.io)

Since Java 7, you can do it in one line even without using any external libraries:从 Java 7 开始,即使不使用任何外部库,您也可以一行完成:

Files.copy(inputStream, outputPath, StandardCopyOption.REPLACE_EXISTING);

See the API docs .请参阅API 文档

Create a temp file first using org.apache.commons.io.首先使用 org.apache.commons.io 创建一个临时文件。

File tempFile = File.createTempFile(prefix, suffix);
tempFile.deleteOnExit();
FileOutputStream out = new FileOutputStream(tempFile);
IOUtils.copy(in, out);
return tempFile;

Easy Java 9 solution with try with resources block尝试使用资源块的简单 Java 9 解决方案

public static void copyInputStreamToFile(InputStream input, File file) {  

    try (OutputStream output = new FileOutputStream(file)) {
        input.transferTo(output);
    } catch (IOException ioException) {
        ioException.printStackTrace();
    }

}

java.io.InputStream#transferTo is available since Java 9. java.io.InputStream#transferTo从 Java 9 开始可用。

If you do not want to use other libraries, here is a simple function to copy data from an InputStream to an OutputStream .如果您不想使用其他库,这里有一个将数据从InputStream复制到OutputStream的简单函数。

public static void copyStream(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}

Now, you can easily write an Inputstream into a file by using FileOutputStream -现在,您可以使用FileOutputStream轻松地将Inputstream写入文件 -

FileOutputStream out = new FileOutputStream(outFile);
copyStream (inputStream, out);
out.close();

If you are using Java version 7 or higher, you can use try-with-resources to properly close the FileOutputStream .如果您使用的是 Java 版本 7 或更高版本,则可以使用try-with-resources正确关闭FileOutputStream The following code use IOUtils.copy() from commons-io .以下代码使用commons-io中的IOUtils.copy()

public void copyToFile(InputStream inputStream, File file) throws IOException {
    try(OutputStream outputStream = new FileOutputStream(file)) {
        IOUtils.copy(inputStream, outputStream);
    }
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM