1. Problem
When the queried data is exported as an xls file (UTF-8 encoding), the data is normal; but when it is exported as a CSV file, the Chinese garbled characters are also encoded in UTF-8. When exported in GBK encoding, the Chinese display is normal.
I thought the problem was solved. When I exported data containing Latin characters (such as ÀÆÊàÌ) later, the data was displayed normally when exported as an xls file. When exported as a CSV file, the Latin characters in the file were displayed as "?".
Trying to change to other encoding methods didn't work. I found a solution to this problem online.
2. Solution
The file exported in CSV mode does not contain BOM information by default. By setting the BOM identifier (byte stream starting with EF BB BF) for the content to be output, that is This problem can be solved. The specific method is as follows: (Recommended: java video tutorial)
... OutputStreamWriter outputStreamWriter = new OutputStreamWriter(response.getOutputStream(), "UTF-8"); // 要输出的内容 result = (String)contentMap.get(RESPONSE_RESULT); response.setHeader("Content-Disposition", "attachment;filename=test.csv"); outputStreamWriter.write(new String(new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF})); outputStreamWriter.write(result); outputStreamWriter.flush();
If it is implemented with OutputStream stream, the parameters can be modified as follows:
out = response.getOutputStream(); //加上UTF-8文件的标识字符 out.write(new byte []{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF});
Note:
BOM: Byte Order Mark, byte order mark (the following is taken from Baidu Encyclopedia)
In UCS encoding, there is one called "Zero Width No-Break Space", the Chinese translation is "Zero Width No-Break Space" " character, its encoding is FEFF. FFFE is a character that does not exist in UCS, so it should not appear in actual transmission.
The UCS specification recommends that we transmit the characters "Zero Width No-Break Space" before transmitting the byte stream. In this way, if the receiver receives FEFF, it indicates that the byte stream is Big-Endian; if it receives FFFE, it indicates that the byte stream is Little-Endian. Therefore the character "Zero Width No-Break Space" is also called BOM.
UTF-8 does not require a BOM to indicate the byte order, but you can use a BOM to indicate the encoding method. The UTF-8 encoding for the character "Zero Width No-Break Space" is EF BB BF. So if the receiver receives a byte stream starting with EF BB BF, it knows that it is UTF-8 encoded. Windows uses BOM to mark the encoding of text files.
For more java knowledge, please pay attention to the java basic tutorial column.
The above is the detailed content of Introduction to the solution to garbled csv exported by java. For more information, please follow other related articles on the PHP Chinese website!

The article discusses various Java garbage collection algorithms (Serial, Parallel, CMS, G1, ZGC), their performance impacts, and suitability for applications with large heaps.

The article discusses the Java Virtual Machine (JVM), detailing its role in running Java programs across different platforms. It explains the JVM's internal processes, key components, memory management, garbage collection, and performance optimizatio

Java's Nashorn engine enables JavaScript scripting within Java apps. Key steps include setting up Nashorn, managing scripts, and optimizing performance. Main issues involve security, memory management, and future compatibility due to Nashorn's deprec

Java's try-with-resources simplifies resource management by automatically closing resources like file streams or database connections, improving code readability and maintainability.

Java enums represent fixed sets of values, offering type safety, readability, and additional functionality through custom methods and constructors. They enhance code organization and can be used in switch statements for efficient value handling.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use