fileName = new String((fileName).getBytes("UTF-8"), "ISO8859_1")+Dates.formatDateTime_New(new Date());
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=" + fileName.replace(" ", "_") + ".xls");
UTF-8文件名正常显示,gb2312中文乱码,所以要用UTF-8
以下两种代码都是可以的
private void initResponseHeader(HttpServletResponse response) throws UnsupportedEncodingException {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("办赛申请");
stringBuilder.append("_");
stringBuilder.append(DateUtil.formatByPattern(Instant.now().toEpochMilli(), DateUtil.DATE_FORMAT));
stringBuilder.append("_");
stringBuilder.append(UUIDUtil.getUUID());
stringBuilder.append(".xlsx");
//解决中文文件名不显示
String fileName = new String(stringBuilder.toString().getBytes("UTF-8"), "iso8859-1") ;
//设置Http响应头告诉浏览器下载这个附件
response.setHeader("Content-disposition", "attachment; filename=" + fileName);
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("UTF-8");
}
private void initResponseHeader(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("企业用户");
stringBuilder.append("_");
stringBuilder.append(DateUtil.formatByPattern(Instant.now().toEpochMilli(), DateUtil.DATE_FORMAT));
stringBuilder.append("_");
stringBuilder.append(UUIDUtil.getUUID());
stringBuilder.append(".xlsx");
//解决中文文件名不显示
String fileName = new String(stringBuilder.toString().getBytes("UTF-8"), "iso8859-1") ;
response.setContentType("application/octet-stream");
response.setContentType("application/x-download");
//设置Http响应头告诉浏览器下载这个附件
response.setHeader("Content-disposition", "attachment; filename=" + fileName);
response.setCharacterEncoding("UTF-8");
}