在Java中将InputStream写入文件

3
我正在编写一个方法,可以将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文件中吗? - UdayKiran Pulipati
@pudaykiran 不,我是在复制后提取的。 - tadddkldkl
你知道Java的Files.copy方法已经可以实现这个功能了吗? - VGR
1个回答

4

大多数情况下,您会因为将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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接