已經確認資料來源編碼沒問題,log也可以正常顯示中文字
但Servlet頁面一直顯示問號???
解決方式:
需在response.getWriter()的之前加入以下設定
response.setContentType("text/html;charset=UTF-8");
如果使用Big5加入
response.setContentType("text/html;charset=Big5");
已經確認資料來源編碼沒問題,log也可以正常顯示中文字
但Servlet頁面一直顯示問號???
解決方式:
需在response.getWriter()的之前加入以下設定
response.setContentType("text/html;charset=UTF-8");
如果使用Big5加入
response.setContentType("text/html;charset=Big5");
InputStream xmlInputStream = new ByteArrayInputStream(
DocumentBuilderFactory xmlFactory = DocumentBuilderFactory.newInst
DocumentBuilder xmlBuilder = xmlFactory.newDocumentBuilder(
InputSource is = new InputSource(xmlInputStream);
is.setEncoding("Big5");
Document xmldoc = xmlBuilder.parse(is);
備註:
不設定setEncoding會遇到的錯誤訊息是
英文:Invalid byte 1 of 1-byte UTF-8 sequence
中文:1-byte UTF-8 序列的無效位元組1
https://openhome.cc/Gossip/Encoding/JVMEncoding.html
有些 API 若不指定編碼,通常會使用 JVM 預設編碼,預設會與作業系統預設編碼相同,像是 String 建構式、 getBytes() 方法或這邊看到的 FileReader 等(其他還有 java.io、java.util、java.net 等套件中的一些 API),可以使用 Charset.defaultCharset() 取得預設編碼。
在啟動 JVM 時,其實可以使用 -Dfile.encoding 指定 JVM 預設編碼,例如:
C:\workspace>java -Dfile.encoding=UTF-8 cc.openhome.Main
getBytes() encodes a String into a byte array using the platform's default charset
https://www.baeldung.com/string/get-bytes
https://openhome.cc/Gossip/Encoding/String.html
ref:
https://openhome.cc/Gossip/Encoding/index.html
https://www.javaworld.com.tw/jute/post/view?bid=21&id=282136&tpg=1&ppg=1&sty=1&age=0#282136
https://stackoverflow.com/questions/12659417/why-does-javas-string-getbytes-uses-iso-8859-1
https://docs.oracle.com/javase/tutorial/i18n/text/string.html