Давайте сначала кое-что проясним: атрибут encoding
в файле XML - всего лишь индикатор для читателя. Это не влияет на количество байтов, записанных в фактический файл.
Итак, давайте упростим пример кода до одного символа á
:
á Результирующие файлы
UTF-8. xml
![utf](https://i.stack.imgur.com/s9JS0.png)
ISO-8859-1.xml
![iso](https://i.stack.imgur.com/wHvWg.png)
This is exactly what we expected. Neither cfxml
nor cffile
/fileWrite
is the issue. So how come you might not get the same result with the above code on your machine?
The problem: Page encoding
When ColdFusion parses template files (.cfm
) and component files (.cfc
), it will use the JVM's default encoding, which, unless specified otherwise, is the system's default encoding. This is also the reason, why everyone can get different results with the above code.
If you have a literal such as á
in a file, this character is encoded using whatever you told your text editor to use. Let's assume that's UTF-8
. If you inspect the file, you will see that the character is properly stored. However, when ColdFusion opens this file and parses the literal, it will assume the character to be encoded with the system's default encoding. And unfortunately you seem to run a system that doesn't or cannot use UTF-8 as systemwide codeset (Windows for example).
Solutions
An (ugly) way to solve it
cfprocessingdirective
(Хакерский) способ решить эту проблему
Сохраните каждый файл, которого ColdFusion затрагивает своим парсером (все .cfm
/ .cfc
файлы), как UTF-8 с BOM . Когда ColdFusion встречает эти байты в начале файла, он вынужден использовать UTF-8, потому что это подразумевает спецификация.
Глобальный способ решения
Добавить -Dfile.encoding=UTF-8
в ваш ColdFusion JVM. Здесь можно добавить параметр: /cfusion/bin/jvm.config
(строка: java.args=
)
Для этого требуется перезапуск ColdFusion. Затем все ваши файлы можно сохранить как простой UTF-8 (без спецификации), и он будет работать нормально.