【发布时间】:2013-08-14 15:29:41
【问题描述】:
我正在编写一个 REST API。我从数据库中获取数据并将其存储在字符串中。然后我像这样将它发送回浏览器:
//fetch data from database
String s = ...
// Prepare it for browser
byte[] data = s.getBytes("UTF-8");
// Send it to browser now
out.write(data); // out is an OutputStream received from Jersey through the MessageBodyWriter interface
数据来自一个 UTF8 编码的 postgresql 数据库。数据存储在一个字符变数(5000)中。
当浏览器(Chrome)显示字符串时出现此错误:
error on line 29 at column 285: Input is not proper UTF-8, indicate encoding !
Bytes: 0x19 0x4C 0x29 0x20
同样的问题出现在其他浏览器上。
以下是发送到浏览器的标头:
200
Content-Type: application/xhtml+xml;charset=UTF-8
摘要 这是数据流的摘要
数据库(UTF8)>休眠>对象>getBytes(“UTF-8”)>浏览器
我错过了什么?
【问题讨论】:
-
@assylias 如何添加标题?
-
@assylias - utf-8 不需要 bom。
-
返回浏览器时是否指定了Content-Type header?以及您如何返回该“数据”数组。 (显示更多代码)
-
你为什么首先向浏览器发送字节[]?您没有类似 HttpServletResponse.getOutputStream.println(s) 的东西,您可以将编码留给其他人吗?
-
@jtahlborn 我更新了我的帖子
标签: java hibernate utf-8 character-encoding jersey