/**
* 文件导出
* @param file 需要导出的文件
* @param response HttpServletResponse
*/
public static void exportFile(File file, HttpServletResponse response) {
response.setHeader("Content-Disposition", "attachment;fileName=" + file.getName());
FileInputStream inputStream = null;
ServletOutputStream outputStream = null;
try {
inputStream = new FileInputStream(file);
outputStream = response.getOutputStream();
int b = 0;
byte[] buffer = new byte[8192];
while (b != -1) {
b = inputStream.read(buffer);
outputStream.write(buffer, 0, b);
}
outputStream.flush();
} catch (IOException e) {
LOGGER.error("返回html文件流失败", e);
} finally {
if (null != outputStream) {
try {
outputStream.close();
inputStream.close();
} catch (IOException e) {
LOGGER.error("关闭流失败", e);
}
}
}
}
java 文件下载,导出
最新推荐文章于 2023-11-28 15:50:47 发布