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

时间:2014-09-02 08:31:15

标签: java dwr

我想知道这两行代码块之间的区别。

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

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

任何一个优点和缺点都欢迎清除细节。 在此先感谢。

1 个答案:

答案 0 :(得分:2)

FileTransfer有多个构造函数,它们需要不同的参数。

您的第一个示例调用构造函数,该构造函数将内容作为字节数组(byte[])。

您的第二个示例调用带有InputStream的构造函数,并将从传递的InputStream中读取内容。

如果您的文件很大,显然不要使用第一个文件,因为它需要将整个文件读入内存。

第二种方法似乎在所有情况下都更好,除非你还需要文件内容,那么你必须阅读两次。