public void downloadFile(HttpServletResponse response, String fullfileName) throws Exception {
//fullfileName是已经生成excel文件(存在数据的)的路径名 例如:e://file/template/454566412221.xls
if (fullfileName == null || fullfileName.equals("")) return;
File file = null;
try {
file = new File(fullfileName);
FileInputStream fileInputStream=new FileInputStream(file);
DataInputStream dataInputStream = new DataInputStream(fileInputStream);
byte[] downContext= new byte[dataInputStream.available()];
dataInputStream.read(downContext);
fileInputStream.close();
dataInputStream.close();
response.reset();
response.setContentType("application/octet-stream;charset=UTF-8");//设置内容类型
response.setHeader("Content-Length", String.valueOf(file.length()));//获取文件的大小
response.setHeader("Content-Disposition", "attachment; filename="+getFileName()+".xls");//显示导出之后的文件名,调用下面的方法
OutputStream outputStream = response.getOutputStream();//导出文件
outputStream.write(downContext);//将数据导出到客户端内存中
outputStream.flush();
outputStream.close();
} catch (Exception e) {
throw new ApplicationException(e.getMessage());
} finally {
if (file.exists()) {
file.delete();
}
}
}
//被上面的方法调用
public static String getFileName() {
return String.valueOf(DatetimeUtil.getCurrentStringDatetime("yyyyMMddHHmmssSSSS"));//这个是封装类,可能你们那里没有
}