[英]Java String Encoding to UTF-8
I have some HTML code that I store in a Java.lang.String variable. 我有一些HTML代码,我存储在Java.lang.String变量中。 I write that variable to a file and set the encoding to UTF-8 when writing the contents of the string variable to the file on the filesystem.
我将该变量写入文件,并在将字符串变量的内容写入文件系统上的文件时将编码设置为UTF-8。 I open up that file and everything looks great eg → shows up as a right arrow.
我打开那个文件,一切看起来很棒,例如→显示为右箭头。
However, if the same String (containing the same content) is used by a jsp page to render content in a browser, characters such as → show up as a question mark (?) 但是,如果jsp页面使用相同的String(包含相同的内容)来在浏览器中呈现内容,则诸如→之类的字符将显示为问号(?)
When storing content in the String variable, I make sure that I use: 在String变量中存储内容时,我确保使用:
String myStr = new String(bytes[], charset)
instead of just: 而不只是:
String myStr = "<html><head/><body>→</body></html>";
Can someone please tell me why the String content gets written to the filesystem perfectly but does not render in the jsp/browser? 有人可以告诉我为什么String内容完美地写入文件系统但不在jsp /浏览器中呈现?
Thanks. 谢谢。
but does not render in the jsp/browser?
但是没有在jsp /浏览器中呈现?
You need to set the response encoding as well. 您还需要设置响应编码。 In a JSP you can do this using
在JSP中,您可以使用
<%@ page pageEncoding="UTF-8" %>
This has actually the same effect as setting the following meta tag in HTML <head>
: 这实际上与在HTML
<head>
设置以下元标记的效果相同:
<meta http-equiv="content-type" content="text/html; charset=utf-8">
Possibilities: 可能性:
Content-Type: text/html; charset=utf-8
Content-Type: text/html; charset=utf-8
Content-Type: text/html; charset=utf-8
in your HTTP Headers. Content-Type: text/html; charset=utf-8
。 The lazy developer (=me) uses Apache Common Lang StringEscapeUtils.escapeHtml http://commons.apache.org/lang/api-release/org/apache/commons/lang/StringEscapeUtils.html#escapeHtml(java.lang.String) which will help you handle all 'odd' characters. 懒惰的开发人员(= me)使用Apache Common Lang StringEscapeUtils.escapeHtml http://commons.apache.org/lang/api-release/org/apache/commons/lang/StringEscapeUtils.html#escapeHtml(java.lang.String)这将帮助您处理所有'奇怪'字符。 Let the browser do the final translation of the html entities.
让浏览器对html实体进行最终翻译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.