Problems: The exported excel file name in the Chrome browser did not have Chinese garbled characters. When testing the IE browser, the exported file name was garbled.
Solution:
Original code:
try { response.setContentType("application/vnd.ms-excel;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); response.addHeader("Content-Disposition", "attachment;filename=" + new String((edTemplate.getTemplateName() + "导入模板").getBytes(), "ISO-8859-1") + ".xls"); OutputStream os = response.getOutputStream(); workbook.write(os); os.flush(); os.close(); } catch (IOException e) { e.printStackTrace(); return ResponseMsgUtil.failure(); }
In new String((edTemplate.getTemplateName() "Import template").getBytes(), " Add a code to the getBytes() method of ISO-8859-1")
Modified code
try { response.setContentType("application/vnd.ms-excel;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); response.addHeader("Content-Disposition", "attachment;filename=" + new String((edTemplate.getTemplateName() + "导入模板").getBytes("gb2312"), "ISO-8859-1") + ".xls"); OutputStream os = response.getOutputStream(); workbook.write(os); os.flush(); os.close(); } catch (IOException e) { e.printStackTrace(); return ResponseMsgUtil.failure(); }
For more java knowledge, please pay attention tojava basic tutorial column.
The above is the detailed content of Solution to garbled file name in java export file. For more information, please follow other related articles on the PHP Chinese website!