SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
String fileName = format.format(new Date());
FileInputStream input = null;
ServletOutputStream outputStream = null;
File file = null;
try {
file = new File(filePath);
input = new FileInputStream(file);
response.setContentType("application/x-download");
response.setHeader("Content-disposition", "attachment; filename="
+ fileName + "_workPlan.xls");
outputStream = response.getOutputStream();
byte[] b = new byte[1024];
int i = 0;
while ((i = input.read(b)) != -1) {
outputStream.write(b, 0, i);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
input.close();
outputStream.flush();
outputStream.close();
// 删除临时文件
if (file.exists() && file.isFile()) {
file.delete();
}
} catch (IOException e) {
e.printStackTrace();
}
}
file download
最新推荐文章于 2022-06-27 09:12:50 发布