问题描述
- Java程序,我建立个xml报文先转换成GBK,之后加密,再用UTF8传输发给外系统,中文出现乱码!
-
Document d = DocumentHelper.createDocument();
Element transaction = d.addElement("transaction"); //根节点
Element header = transaction.addElement("header"); //报文头
header.addElement("transcode").setText(transcode); //交易码(整合平台)
header.addElement("transagency").setText(transagency); //基层行社机构代号(整合平台)
header.addElement("businumb").setText(businumb); //基层行社业务代号(整合平台)
header.addElement("transteller").setText(transteller); //交易柜员
header.addElement("transchannel").setText(transchannel); //交易渠道
header.addElement("transserial").setText(transserial); //交易流水号
header.addElement("checktag").setText(""); //校验字串
Element body = transaction.addElement("body"); //报文体
Element request = body.addElement("request"); //报文请求
if( requestMap!=null ){
Set keys = requestMap.keySet();
//logger.debug("keys>>["+keys+"]");
for( String key : keys ){
//logger.debug("key:["+key+":"+requestMap.get(key)+"]");
if(requestMap.get(key)!=null){
request.addElement(key).setText(requestMap.get(key));
}else{
request.addElement(key).setText("");
}
}
}// Map ictrl1Map = new HashMap();
// ictrl1Map = (Map)((Map) requestMap.get("ICTRL1"));
// if(requestMap.get("ICTRL1")!=null){
// Element ictrl1 = request.addElement("ICTRL1"); //报文请求(公共输入控制信息1)
// Set keys = ictrl1Map.keySet();
// logger.debug("keys:["+keys+"]");
// for( String key : keys ){
// logger.debug("key:["+key+"]");
// ictrl1.addElement(key).setText((String) ictrl1Map.get(key));
// }
//// }
if( ictl1Map!=null ){
Set ictl1keys = ictl1Map.keySet();
//logger.debug("ictl1keys>>"+ictl1keys);
if(!ictl1keys.isEmpty()){
Element ictrl1 = request.addElement("ICTRL1"); //报文请求(公共输入控制信息1)
for( String ictl1key : ictl1keys ){
//logger.debug("ictl1key:["+ictl1key+":"+ictl1Map.get(ictl1key)+"]");
ictrl1.addElement(ictl1key).setText(replaceNullString((String) ictl1Map.get(ictl1key)));
}
}
}
if( ictl2Map!=null ){
Set ictrl2keys = ictl2Map.keySet();
//logger.debug("ictrl2keys>>"+ictrl2keys);
if(!ictrl2keys.isEmpty()){
Element ictrl2 = request.addElement("ICTRL2"); //报文请求(公共输入控制信息1)
for( String ictrl2key : ictrl2keys ){
//logger.debug("ictrl2key:["+ictrl2key+":"+ictl2Map.get(ictrl2key)+"]");
ictrl2.addElement(ictrl2key).setText(replaceNullString((String) ictl2Map.get(ictrl2key)));
}
}
}OutputFormat out = OutputFormat.createPrettyPrint();////设置XML文档输出格式 out.setNewLineAfterDeclaration(false); out.setEncoding(Constants.CHARSET_GBK);//设置XML文档的编码类型GBK // out.setIndent(true); //设置是否缩进 // out.setIndent(" "); //设置以空格方式实现缩进,可兼容文本编辑 // out.setNewlines(true); //设置是否换行 ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLWriter w = new XMLWriter(baos,out); w.write(d); w.close(); String msg = null; msg = new String(baos.toByteArray(),Constants.CHARSET_GBK);//GBK logger.info("@[buildCommbuf]加密前交易报文GBK:n"+msg);
/***Modified in 20150504 for zhptdes
byte[] mw = DesUtil.encrypt(baos.toByteArray(), this.getPassword());
// logger.debug("@mw:n["+Arrays.toString(mw)+"]");
String hexMw = DesUtil.bytes2hex(mw);
****/
/*Add for msgTrace test*/
msgTrace(transserial, transcode, transteller, msg);
/*Add for msgTrace test*///加密处理 String hexMw = null;
// logger.debug("--pincrypt["+pincrypt+"]makcrypt["+makcrypt+"]");
if(transcode.equals(Constants.TRANID_MYCZ)){ //800009密钥重置申请
logger.info("[parseCommbuf]交易码:"+transcode+"依据PINCRYPTBLOCK["+pincrypt+"]加密"+new String(baos.toByteArray(),Constants.CHARSET_GBK));
hexMw = DesHostUtil.hostEncrypt(new String(baos.toByteArray()),pincrypt);
}else{
logger.info("[parseCommbuf]交易码:"+transcode+"依据MAKCRYPTBLOCK["+makcrypt+"]加密");
hexMw = DesHostUtil.hostEncrypt(new String(baos.toByteArray()),makcrypt);
}
解决方案
xml报文先转换成utf-8试试呢?
解决方案二:
GBK你用UTF-8不是本来就会出现问题么。。。。