Home > Java > JavaBase > body text

Solution to garbled file name in java export file

Release: 2019-12-14 14:06:51
Original
3824 people have browsed it

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();
        }
Copy after login

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();
        }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template