简体   繁体   English

用Java将InputStream写入文件

[英]Writing an InputStream to a File in Java

I am writing this method which will convert an InputStream into a file, but the method is working for some of the inputs but not for all (when i pass a zip file as an InputStream it works). 我正在编写此方法,它将InputStream转换为文件,但是该方法适用于某些输入,但不适用于所有输入(当我将zip文件作为InputStream传递时,它可以工作)。 It simply creates a file with 0 bytes. 它只是创建一个0字节的文件。

I know that this question has been asked before, before anyone mark it as duplicate please go through what i have done so far.. 我知道这个问题已经被问过了,在有人将其标记为重复之前,请仔细阅读我到目前为止所做的事情。

This is what I have tried so far. 到目前为止,这是我尝试过的。 Most of these solutions are found from StackOverFlow. 这些解决方案中的大多数都可以从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();

        ........
}

The most of your ways damage the file because of you read InputStream to a String variable (binary files will be damaged). 由于将InputStream读为String变量,大多数方法都会损坏文件(二进制文件将被损坏)。 Try this implementation: 试试这个实现:

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

or 要么

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

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

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