字节流
//读取文件,资源路径,本文以图片为例子,其中xxx为资源的绝对路径
FileInputStream fileInputStream = new FileInputStream("xxx/b.png");
//获取字节响应输出流
ServletOutputStream outputStream = resp.getOutputStream();
- 第一种读取文件流方式:循环遍历
byte[] bytes = new byte[1024];
int len = 0;
while ((len = fileInputStream.read(bytes)) != -1){
outputStream.write(bytes,0,len);
}
- 第二种依赖第三方工具
首先导入依赖坐标comments-io
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
//利用第三方工具实现copy的输入流到输出流
IOUtils.copy(fileInputStream,outputStream);
//关闭文件输入流
fileInputStream.close();