//反流(方法的返回参数必须是void)
//读取指定路径下面的文件
InputStream in = new FileInputStream("xxxx.docx");
ServletOutputStream outputStream = null;
response.reset();
response.setContentType("application/ostet-stream");
outputStream = response.getOutputStream();
//创建存放文件内容的数组
byte[] buff =new byte[1024];
//所读取的内容使用n来接收
int n;
//当没有读取完时,继续读取,循环
while((n=in.read(buff))!=-1){
//将字节数组的数据全部写入到输出流中
outputStream.write(buff,0,n);
}
//强制将缓存区的数据进行输出
outputStream.println();
//关流
outputStream.close();
in.close();
后端返回文件流
最新推荐文章于 2023-11-10 10:44:15 发布