【问题标题】:Writing an InputStream to a File in Java在 Java 中将 InputStream 写入文件
【发布时间】:2016-03-19 21:30:43
【问题描述】:

我正在编写此方法,它将 InputStream 转换为文件,但该方法适用于某些输入,但不适用于所有输入(当我将 zip 文件作为 InputStream 传递时,它可以工作)。它只是创建一个 0 字节的文件。

我知道之前有人问过这个问题,在有人将其标记为重复之前,请先检查一下我到目前为止所做的事情..

这是我迄今为止尝试过的。这些解决方案中的大多数都可以从 StackOverFlow 中找到。

private void saveFile(String filename, InputStream input) throws IOException 
{
        if (filename == null) {
            return;
        }


        File file = new File(filename);


        //*************** 1st *****************

        String stringFromInputStream = IOUtils.toString(input, "UTF-8");
        System.out.println("This is inputStream:"+stringFromInputStream);
        FileUtils.writeStringToFile(file, StringFromInputStream);


        //********************2nd **********************
        String stringFromInputStream = IOUtils.toString(input, "UTF-8");
        byte[] aByte = StringFromInputStream.getBytes();
        FileOutputStream fos=new FileOutputStream(file);
        IOUtils.write(aByte, fos);


        //*******************3rd**********************
        String stringFromInputStream = IOUtils.toString(input, "UTF-8");
        byte[] aByte = StringFromInputStream.getBytes();
        FileOutputStream fos=new FileOutputStream(file);
        fos.write(aByte);


        //*********************4th*********************
        String stringFromInputStream = IOUtils.toString(input, "UTF-8");
        FileWriter fr=new FileWriter(file);
        BufferedWriter br=new BufferedWriter(fr);
        br.write(StringFromInputStream);
        br.flush();
        fr.flush();
        br.close();
        fr.close();



        //*************5th**********************
        String stringFromInputStream = IOUtils.toString(input, "UTF-8");
        Files.write(Paths.get(filename), StringFromInputStream.getBytes());


        //************************6th**************
        String stringFromInputStream = IOUtils.toString(input, "UTF-8");
        FileUtils.writeStringToFile(file, StringFromInputStream);


        //******************7th*********************
        String stringFromInputStream = IOUtils.toString(input, "UTF-8");
        FileWriter fileWriter = new FileWriter(file);
        PrintWriter printWriter = new PrintWriter(fileWriter);
        printWriter.print(StringFromInputStream);
        fileWriter.flush();
        fileWriter.close();


        //**********************8th*************************
        final Path destination = Paths.get(filename);
        try (
            final InputStream in = input;
        ) {
            Files.copy(in, destination,StandardCopyOption.REPLACE_EXISTING);
        }


        // ************* This one works but not for all(Sometimes creats a file with 0 bytes) ****************

        FileOutputStream fos = new FileOutputStream(file);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        BufferedInputStream bis = new BufferedInputStream(input);
        int aByte;
        while ((aByte = bis.read()) != -1) {
            bos.write(aByte);
        }
        bos.flush();
        fos.flush();
        bos.close();
        fos.close();
        bis.close();

        ........
}

【问题讨论】:

  • takunnithan 是否要将新文件添加到 zip 文件中?
  • @pudaykiran 不,我复制后解压。
  • 你知道 Java 的 Files.copy 方法已经这样做了吗?

标签: java file stream inputstream filewriter


【解决方案1】:

由于您将 InputStream 读取到 String 变量(二进制文件将被损坏),因此您的大多数方式都会损坏文件。试试这个实现:

byte[] aByte = IOUtils.toByteArray(input);
FileOutputStream fos=new FileOutputStream(file);
IOUtils.write(aByte, fos);

FileOutputStream fos=new FileOutputStream(file);
IOUtils.copy(input, fos);

【讨论】:

    猜你喜欢
    • 2014-05-05
    • 2016-12-22
    • 2016-07-20
    • 2012-04-12
    • 2012-04-25
    • 2013-09-25
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    相关资源
    最近更新 更多