需求:后端直接传输文件的流给前端处理
- 一、
- public void transfer(HttpServletResponse res) throws Exception {
- File f = new File(path);//path 文件的相对路径
- ServletOutputStream outputStream = res.getOutputStream();
- outputStream.write(FileUtil.readBytes(f));//hutool 工具类 FileUtil
- //强制将缓存区的数据进行输出
- outputStream.flush();
- //关流
- outputStream.close();
- }
-
- 二、
- public void transfer(HttpServletResponse res) throws Exception {
- File f = new File(path);//path 文件的相对路径
- nputStream in = new FileInputStream(f);
- ServletOutputStream outputStream = res.getOutputStream();
- byte[] buff = new byte[1024];
- int n;
- while ((n = in.read(buff)) != -1) {
- //将字节数组的数据全部写入到输出流中
- outputStream.write(buff, 0, n);
- }
- //强制将缓存区的数据进行输出
- outputStream.flush();
- //关流
- outputStream.close();
- }