多字节字符损坏为???当使用 HTTPURLConnection 从数据库读取并发布到 ASP 页面时

发布于 2024-10-18 07:36:37 字数 948 浏览 6 评论 0原文

在我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流绪微梦 2024-10-25 07:36:37

那里正在发生很多潜在的转化。您应该在每个步骤中验证数据:

  • 检查是否正确从数据库中获取数据
  • 查看转换后的 XML 的外观
  • 观察网络上传输的内容(包括 HTTP 标头)
  • 中获得的内容

准确检查您在ASP 不只是打印出字符串 as 字符串 - 通过将每个字符转换为 int 来记录每个字符的 Unicode 值:

for (int i = 0; i < text.length(); i++)
{
    char c = text.charAt(i);
    log("Character " + c + " - " + (int) c);
}

There are lots of potential conversions going on there. You should verify the data at every step:

  • Check that you're getting it out of the database correctly
  • See what the transformed XML looks like
  • Watch what goes over the network (including HTTP headers)
  • Check exactly what you're getting in ASP

Don't just print out the strings as strings - log the Unicode value of each character, by casting it to int:

for (int i = 0; i < text.length(); i++)
{
    char c = text.charAt(i);
    log("Character " + c + " - " + (int) c);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文