简体   繁体   English

字节[]和FileTranfer(DWR)中的InputStream之间的区别

[英]Difference between Byte[] and InputStream in FileTranfer (DWR)

I wanted to know about the difference between these two block of lines of codes. 我想知道这两行代码块之间的区别。

 byte[] fileBytes = FileUtils.readFileToByteArray(new File(completeFilePath.toString()));
  ..
 return new FileTransfer(errorFileName, "application/vnd.ms-excel", is);

and

 File csvFile = new File(completeFilePath.toString());
 InputStream is = new BufferedInputStream(new FileInputStream(csvFile));
 return new FileTransfer(errorFileName, "application/vnd.ms-excel", is);

any advantage and disadvantage for either of them is welcome to clear out detail. 我们欢迎您清除其中任何一个的优缺点。 Thanks in Advance. 提前致谢。

FileTransfer has multiple constructors which expect different parameters. FileTransfer具有多个构造函数,它们期望使用不同的参数。

Your first example calls the constructor which takes the content as a byte array ( byte[] ). 第一个示例调用构造函数,该构造函数将内容作为字节数组( byte[] )。

Your second example calls the constructor which takes an InputStream and will read the content itself from the passed InputStream . 您的第二个示例调用构造函数,该构造函数接受一个InputStream并将从传递的InputStream读取内容本身。

If your file is big, obviously don't use the first one because it requires the whole file to be read into the memory. 如果文件很大,则显然不要使用第一个文件,因为它需要将整个文件读入内存。

The second approach seems better in all cases except if you also need the file content, then you would have to read it twice. 在所有情况下,第二种方法似乎都更好,除非您还需要文件内容,则必须将其读取两次。

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

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