使用Java调用.net写的WebService传入中文乱码, 请教高手如何解决

owencai 2009-02-16 04:59:36
如题:
我知道是编码不一致的问题, 但具体应该怎样解决就不知道了?

我已试过以下的方法都是不行:
1. 客户端处理
String content = new String (input.getBytes( "ISO-8859-1"), "utf8");
2. 客户端处理
String content = new String (input.getBytes( "utf8"), "utf8");
3. 客户端处理
String content = new String (input.getBytes( "gb2312"), "utf8");
4. 客户端处理
String content = new String (input.getBytes( "utf8"), "gb2312");
5. 客户端处理
String content = new String (input.getBytes( "gb2312"), "gb2312");
6. 改服务器端web.config
<globalization requestEncoding="utf-8" responseEncoding="utf-8" />
7. 客户端处理
用这个函数处理:
public static String Encoding(String text)
{
if (null == text || 0 == text.trim().length())
return "";
StringBuffer sb = new StringBuffer();
char[] chars = text.toCharArray();
for (int i = 0; i < chars.length; i++)
{
if (chars[i] > 127)
{
sb.append("&#x");
sb.append(Integer.toHexString(chars[i]));
sb.append(";");
}
else
{
sb.append(chars[i]);
}
}
return sb.toString();
}

请各位大侠赐教
...全文
1727 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hildaxuhui 2011-11-03
  • 打赏
  • 举报
回复
我表示我也遇到这个问题了...几个星期都木解决
diudiu1987 2009-03-09
  • 打赏
  • 举报
回复
http://www.programfan.com/club/showtxt.asp?id=290603

不知道能不能对你有所帮助
owencai 2009-02-24
  • 打赏
  • 举报
回复
终于解决啦, 发现原来是 utf-8在.net和java里的编码不同, 原因看前面吧, 只要将他们变为Base64就解决了啦, 现在给出代码!

JAVA端将UTF-8转为 base64:
package cls;

import java.io.*;

public class Base64 {
private static char[] base64EncodeChars = new char[] {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/' };

private static byte[] base64DecodeChars = new byte[] {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 };

private Base64() {}

public static String encode(byte[] data) {
StringBuffer sb = new StringBuffer();
int len = data.length;
int i = 0;
int b1, b2, b3;

while (i < len) {
b1 = data[i++] & 0xff;
if (i == len) {
sb.append(base64EncodeChars[b1 >>> 2]);
sb.append(base64EncodeChars[(b1 & 0x3) << 4]);
sb.append("==");
break;
}
b2 = data[i++] & 0xff;
if (i == len) {
sb.append(base64EncodeChars[b1 >>> 2]);
sb.append(
base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]);
sb.append(base64EncodeChars[(b2 & 0x0f) << 2]);
sb.append("=");
break;
}
b3 = data[i++] & 0xff;
sb.append(base64EncodeChars[b1 >>> 2]);
sb.append(
base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]);
sb.append(
base64EncodeChars[((b2 & 0x0f) << 2) | ((b3 & 0xc0) >>> 6)]);
sb.append(base64EncodeChars[b3 & 0x3f]);
}
return sb.toString();
}

public static byte[] decode(String str) {
byte[] data = str.getBytes();
int len = data.length;
ByteArrayOutputStream buf = new ByteArrayOutputStream(len);
int i = 0;
int b1, b2, b3, b4;

while (i < len) {

/* b1 */
do {
b1 = base64DecodeChars[data[i++]];
} while (i < len && b1 == -1);
if (b1 == -1) {
break;
}

/* b2 */
do {
b2 = base64DecodeChars[data[i++]];
} while (i < len && b2 == -1);
if (b2 == -1) {
break;
}
buf.write((int) ((b1 << 2) | ((b2 & 0x30) >>> 4)));

/* b3 */
do {
b3 = data[i++];
if (b3 == 61) {
return buf.toByteArray();
}
b3 = base64DecodeChars[b3];
} while (i < len && b3 == -1);
if (b3 == -1) {
break;
}
buf.write((int) (((b2 & 0x0f) << 4) | ((b3 & 0x3c) >>> 2)));

/* b4 */
do {
b4 = data[i++];
if (b4 == 61) {
return buf.toByteArray();
}
b4 = base64DecodeChars[b4];
} while (i < len && b4 == -1);
if (b4 == -1) {
break;
}
buf.write((int) (((b3 & 0x03) << 6) | b4));
}
return buf.toByteArray();
}
}
.
.
.
.

base64.encode(content.getBytes("utf-8"));



.NET端将Base64转为 UTF-8
Public Function DecodeBase64(ByVal code_type As String, ByVal code As String) As String
Dim decode As String = ""
Dim bytes As Byte() = Convert.FromBase64String(code)
Try
decode = Encoding.GetEncoding(code_type).GetString(bytes)
Catch
decode = code
End Try
Return decode
End Function





这样就解决啦! 哈哈
owencai 2009-02-18
  • 打赏
  • 举报
回复
再顶
owencai 2009-02-18
  • 打赏
  • 举报
回复
在这里, 又贴了一次, 是高分求解啊 http://topic.csdn.net/u/20090218/09/4eadbf26-31cc-4aa7-92f9-3a678efcb315.html?seed=687071173
socool627 2009-02-18
  • 打赏
  • 举报
回复
看看
owencai 2009-02-17
  • 打赏
  • 举报
回复
但我在.net端将字符串分解后的数组的每一位都是63, 为什么呢, 无论输入什么字符都是这样
kf156 2009-02-17
  • 打赏
  • 举报
回复
服务端写个循环,将数组每一位+256就行了。
owencai 2009-02-17
  • 打赏
  • 举报
回复
有谁写出了代码, 共享一吧
owencai 2009-02-17
  • 打赏
  • 举报
回复
有哪位高手能给出代码啊
owencai 2009-02-17
  • 打赏
  • 举报
回复
我也想我是错的确但是真的没错啊
ghzxh 2009-02-17
  • 打赏
  • 举报
回复
不懂,up
netsocket 2009-02-17
  • 打赏
  • 举报
回复
不懂,up
隧便 2009-02-17
  • 打赏
  • 举报
回复
只要把实际字符串长度和转换后的byte数组长度一起传过去,然后在转换就不会有乱码了。
隧便 2009-02-17
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 owencai 的回复:]
但我在.net端将字符串分解后的数组的每一位都是63, 为什么呢, 无论输入什么字符都是这样
[/Quote]


应该不会这样的,除非你的包出错了。java和.net在转换的时候会不一样。从.net端到java端不会有影响,但是java端到.net端 中文的byte[]会自动添加一些字符进去。所以你要把实际的字符串长度也要传到.net端,然后进行转换这样就不会有乱码了。
隧便 2009-02-17
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 owencai 的回复:]
但我在.net端将字符串分解后的数组的每一位都是63, 为什么呢, 无论输入什么字符都是这样
[/Quote]


应该不会这样的,除非你的包出错了。java和.net在转换的时候会不一样。从.net端到java端不会有影响,但是java端到.net端 中文的byte[]会自动添加一些字符进去。所以你要把实际的字符串长度也要传到.net端,然后进行转换这样就不会有乱码了。
owencai 2009-02-17
  • 打赏
  • 举报
回复
自己顶
owencai 2009-02-17
  • 打赏
  • 举报
回复
期待高手中......
kf156 2009-02-16
  • 打赏
  • 举报
回复
刚写了个方法,将非中文字符减小256,打印出来发现值没变...
非中文字符的值都是正的,减小256还是原值
也就是说,客户端不用变,只需在服务端在得到的数组每位加上256即可?
kf156 2009-02-16
  • 打赏
  • 举报
回复
仔细想了下,还是得按文章里写的做
比如“中文测试”
得到每一个字符,对这字符进行getBytes(),如果得到的byte[]长度等于1,说明不是中文字符,则每一位减256
在.net服务端,将收到的数组每位加上256。

本来想直接中文字符加256,服务端就不用变了。想想不行...
加载更多回复(10)

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧