多字节字符损坏为???当使用 HTTPURLConnection 从数据库读取并发布到 ASP 页面时
在我的java代码中,我从数据库检索一些多字节数据并制作一些xml DOM,将该数据作为某个节点的值,然后将DOM转换为字符串并通过 HTTPURLConnection 将字节发布到ASP页面,但不知何故在接收端数据显示为 ???而不是一些多字节值。请建议该怎么做。
我已经在做的事情..
1)我已将 -Dfile.encoding =UTF8
设置为系统属性 2)在使用 TransformerFactory 将 XML DOM 转换为 String 时,我已设置
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8")
以确保编码正确。 请建议我哪里出错了。
@Jon Skeet 这里还需要添加一些东西... 1) 我正确地从数据库获取数据 2) 转换后的 XML 似乎也是正确的,因为我通过将其保存到本地文件系统进行了检查。
对于之前发布的内容,我使用的是类似的东西
'dout = new DataOutputStream(urlconn.getOutputStream());'
'dout.write(strXML.getBytes());'
'dout.write(strXML);'
,接收端的结果数据被转换为??????但后来我切换到
'
dout=new OutputStreamWriter(urlconn.getOutputStream(),"UTF8");'
'dout.write(strXML);'
那么接收端的数据似乎是正确的......但在这种情况下,接收端处理数据的方式会出现问题。在我的接收器 ASP 代码中,我使用 objStream.WriteLine (oXMLDom.xml) ...这里失败并开始给出内部服务器错误...请建议第二种方法有什么问题。
In my java code,I am retrieving some multibyte data from database and making some xml DOM, with that data as the value of some node then converting the DOM to String and posting bytest to ASP Page via HTTPURLConnection , but somehow at receiver end the data is appearing as ???? instead of some multibyte values.Please suggest what to do.
Things that i am already doing..
1) I have set -Dfile.encoding =UTF8
as System Property
2)While using TransformerFactory
for converting my XML DOM to String , i have set
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8")
to make sure that the encoding is proper there.
Please suggest where i am getting wrong.
@Jon Skeet Few more things to add here... 1) I am getting data from database correctly 2) Transformed XML also appears to be proper, as i checked by saving it to my local file system.
For posting earlier i was using something like
'dout = new DataOutputStream(urlconn.getOutputStream());'
'dout.write(strXML.getBytes());'
'dout.write(strXML);'
and the resulting data at the receiver end was getting converted to ????? but then i switched to
'
dout=new OutputStreamWriter(urlconn.getOutputStream(),"UTF8");'
'dout.write(strXML);'
then data at receiver end appears to be proper ... but the problem occurs with the way it is handled at receiver end in this case. in my receiver ASP code i am using objStream.WriteLine (oXMLDom.xml)
... and here it fails and starts to give internal server error... please suggest whats wrong with second approach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那里正在发生很多潜在的转化。您应该在每个步骤中验证数据:
准确检查您在ASP 不只是打印出字符串 as 字符串 - 通过将每个字符转换为
int
来记录每个字符的 Unicode 值:There are lots of potential conversions going on there. You should verify the data at every step:
Don't just print out the strings as strings - log the Unicode value of each character, by casting it to
int
: