web导出文件时遇到文件名中的中文变成了下划线,header头部不能直接写入中文,还需要进行转码,通过new String("记录-".getBytes("gb2312"), "8859_1")将中文转化为 "8859_1"编码。
try {
OutputStream output = response.getOutputStream();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH:mm:ss");
String myfile = sdf.format(new Date());
response.setCharacterEncoding("utf-8");
response.addHeader("Content-Disposition", "inline;filename="+new String("记录-".getBytes("gb2312"), "8859_1")+myfile+".xls");
response.setContentType("application/msexcel");
workbook.write(output);
output.flush();
output.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}