if(null!=page){
try {
getResponse().reset();// 清空输出流
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//"yyyy年MM月dd日HH时mm分ss秒"
String formatFileName =URLEncoder.encode("Refrigerator cold chain alarm information"+ df.format(new Date()) +".xls","UTF-8");
getResponse().setHeader("Content-disposition","attachment; filename="+formatFileName);// 设定输出文件头
getResponse().setContentType("application/msexcel");// 定义输出类型
OutputStream os = getResponse().getOutputStream();
WritableWorkbook book = Workbook.createWorkbook(getResponse().getOutputStream());// 取得输出流
@SuppressWarnings("unchecked")
List<AlarmInfo> mapList = (List<AlarmInfo>) page.getResults();
this.editExcel(mapList,book);
book.write(); // 写入文件
book.close();
os.close(); // 关闭流
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
下载下来的文件空格会变成加号,时间里的冒号会变成 %3A。
我试过将FILENAME 转码后再用replace替换里面的字符的方法,但是结果就无法下载正确格式的文件了。
求助!
更新:使用replace将filename中的加号变成空格的方法,在谷歌上可行,在火狐上就会变成下载一个文件名Refrigerator,下载框中显示类型为 application/msexcel的无后缀文件了