使用ServletOutputStream将音频流输出到客户端、代码莫名其妙执行两次

woshixiaoxiaoqi 2018-12-06 07:08:56

public void test(HttpServletResponse response) throws IOException {
System.out.println("111");
// 使用response获得字节输出
ServletOutputStream out = response.getOutputStream();
//OutputStream out = new FileOutputStream("D://111_copy.mp3");
int i = 0;

InputStream fis = new FileInputStream("D://111.mp3");


byte[] buffer = new byte[fis.available()];
/*while ((len = in.read(buffer)) > 0) {
System.out.println("count:" + (i++) + " len:" + len);
out.write(buffer, 0, len);
}*/
fis.read(buffer);
out.write(buffer);

System.out.println("222");
fis.close();
System.out.println("333");
out.close();
System.out.println("444");
}


运行结果:

代码跑了两次,很莫名其妙,有大神知道原因吗?
...全文
159 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧