Java 字符串编码为 UTF-8
我有一些 HTML 代码存储在 Java.lang.String 变量中。我将该变量写入文件,并将字符串变量的内容写入文件系统上的文件时将编码设置为 UTF-8。我打开该文件,一切看起来都很棒,例如 → 显示为右箭头。
但是,如果 jsp 页面使用相同的字符串(包含相同的内容)在浏览器中呈现内容,则诸如 → 之类的字符会显示为问号 (?)
在将内容存储在字符串变量中时,我确保我使用:
String myStr = new String(bytes[], charset)
而不是仅仅:
String myStr = "<html><head/><body>→</body></html>";
有人可以告诉我为什么字符串内容被完美地写入文件系统但没有在 jsp/浏览器中呈现吗?
谢谢。
I have some HTML code that I store in a Java.lang.String variable. 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. I open up that file and everything looks great e.g. → 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 (?)
When storing content in the String variable, I make sure that I use:
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?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还需要设置响应编码。在 JSP 中,您可以使用以下命令来完成此操作。
这实际上与在 HTML
中设置以下元标记具有相同的效果:
You need to set the response encoding as well. In a JSP you can do this using
This has actually the same effect as setting the following meta tag in HTML
<head>
:可能性:
Content-Type: text/html; HTTP 标头中的 charset=utf-8
。Possibilities:
Content-Type: text/html; charset=utf-8
in your HTTP Headers.懒惰的开发人员(=我)使用 Apache Common Lang StringEscapeUtils.escapeHtml http://commons.apache.org/lang/api-release/org/apache/commons/lang/StringEscapeUtils.html#escapeHtml(java.lang.String)< /a> 这将帮助您处理所有“奇数”字符。让浏览器完成 html 实体的最终翻译。
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. Let the browser do the final translation of the html entities.