日期:2014-05-20  浏览次数:20627 次

JAVA中文编码问题
实现domino server向指定的短信服务器IP地址发短信,已调通!但接收的中文有乱码,即:中文显示为“???”,请大家帮看下,如何解码成中文?非常感谢!

import lotus.domino.*;
import java.net.*;  
import java.io.*;  
   
public class JavaAgent extends AgentBase {
public void NotesMain() {
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
// (Your code goes here)
   
  String urlStr="http://172.172.0.1/mobile/smssend/test.jsp?actiontype=SmsSend";
  String mobile="13800138000";
  String messages="ljh测试短信!";
  messages=java.net.URLEncoder.encode(messages);
  URL url = new URL(urlStr+"&mobile="+mobile+"&message="+messages);
  URLConnection connection = url.openConnection(); 
  connection.connect();
  InputStream out = connection.getInputStream();
  byte[] bytes = new byte[200];
  out.read(bytes);
  System.out.println(new String(bytes,"GBK"));
  System.out.println("OK!");

} catch(Exception e) {
e.printStackTrace();
}
}
}


------解决方案--------------------
Java code
byte[]   bytes   =   new   byte[200]; 
out.read(bytes); 
System.out.println(new String(bytes,connection.getContentEncoding()));// 用反馈回来的编码,也许不是GBK呢

------解决方案--------------------
不要用byte[]去寄存汉字,那样的汉字其实已经抛弃了高8位,试试以char的形式来读取
------解决方案--------------------
String str = new String("中文".getBytes("iso8859-1"),"gb2312")