Home >Java >JavaBase >Solution to garbled file name in java export file

Solution to garbled file name in java export file

尚
Original
2019-12-14 14:06:513883browse

Solution to garbled file name in java export file

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn