解决不同浏览器下处理中文的问题
解决长文件名的问题
fileName = fileName + DateTools.getCurrentDate();
String userAgent = request.getHeader(Constants.USER_AGENT);
String encodeFileName = Tools.encode(userAgent, fileName);
if (encodeFileName.length() > 150 && userAgent.contains("MSIE")) {
//解决中文文件名超过17个汉字的问题
encodeFileName =new String( fileName.getBytes("GBK"), "ISO8859-1" ) ;
}
response.setHeader("Content-Disposition",
"attachment; filename=" + encodeFileName +".xls");
// response.setHeader("Content-Disposition",
// "attachment; filename=" + Tools.toUtf8String("设备信息"+System.currentTimeMillis()+".xls"));
response.setContentType("application/vnd.ms-excel");
OutputStream os = null;
try {
os = response.getOutputStream();
excel.write(os);
} catch (IOException e) {
logger.info("---导出报表失败---" + e.getMessage());
}
public static String encode(String userAgent, String content) {
try {
if (StringUtils.isNotBlank(content)) {
if (userAgent.contains("MSIE")) {
content = URLEncoder.encode(content, "UTF-8");
} else if (userAgent.contains("Opera")) {
} else {
content = "=?UTF-8?B?" + new String(Base64.encodeBase64(content.getBytes("UTF-8")), "UTF-8") + "?=";
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return content;
}