问大家关于java的IO流这边的几个问题,希望能得到你们的答案,谢谢了!
BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
servletOutputStream = httpServletResponse.getOutputStream();
httpServletResponse.setHeader("Content-disposition",
"attachment; filename=" + "results.csv");
httpServletResponse.setContentLength((int) file.length());
httpServletResponse.setContentType("application/csv");
byte[] buffer = new byte[1024];
while (input.read(buffer) > 0) {
servletOutputStream.write(buffer);
}
input.close();
servletOutputStream.close();
httpServletResponse.flushBuffer();
}
这段代码是用来jsp下载文件的。
如果我把上面对应的代码换成下面这样好像也能实现功能。
byte[] buffer = new byte[1024];
int flag = 0;
input = new FileInputStream(exportFile);
while ((flag = input.read(buffer)) > 0) {
servletOutputStream.write(buffer, 0, flag);