Linux服务器报文响应乱码

因为要写一个接口用于接收报文,以及返回报文都用GBK格式,代码如下:

 public void returnMa(HttpServletRequest servletRequest, HttpServletResponse response)throws Exception{

        InputStream inputStream = servletRequest.getInputStream();
        SAXReader saxReader = new SAXReader();
        logger.info("插入的数据未编译前为{}", inputStream.toString());
//        InputStreamReader strInStream = new InputStreamReader(inputStream, "GBK");
        Document read1 = saxReader.read(inputStream);
        Element rootElement = read1.getRootElement();
        HashMap<String, String> map = new HashMap();
        xmlToMap(rootElement,map);
        Document document = DocumentHelper.createDocument();
        document.setXMLEncoding("GBK");
        Element packet = document.addElement("PACKET");
        Element head = packet.addElement("HEAD");
        Element uuid = head.addElement("UUID");
        uuid.addText(map.get("UUID")==null?"":map.get("UUID"));

            if(i>0){
                Element returncode = head.addElement("RETURNCODE");
                returncode.addText("1");
                Element returnmessage = head.addElement("RETURNMESSAGE");
                returnmessage.addText("成功");
            }else{
                Element returncode = head.addElement("RETURNCODE");
                returncode.addText("2");
                Element returnmessage = head.addElement("RETURNMESSAGE");
                returnmessage.addText("失败");
            }
        }else{
            Element returncode = head.addElement("RETURNCODE");
            returncode.addText("2");
            Element returnmessage = head.addElement("RETURNMESSAGE");
            returnmessage.addText("失败");
        }
        response.setCharacterEncoding("GBK");
        response.setContentType("text/plain;charset=GBK");
//
        response.getOutputStream().write(document.asXML().getBytes());
//           String res=new String( document.asXML().getBytes("GBK),"utf-8");
//       return res;

    }
    private static void xmlToMap(Element element, Map<String, String> map) {

        map.put(element.getName().toUpperCase(), element.getTextTrim());

        Iterator iterator = element.elementIterator();
        while (iterator.hasNext()) {
            xmlToMap((Element) iterator.next(), map);
        }
    }

完成之后本地测试正常,放到服务器上测试出现乱码

HTTP/1.1 200 
Content-Type: text/plain;charset=GBK
Content-Length: 144
Date: Thu, 01 Aug 2019 09:27:58 GMT

<?xml version="1.0" encoding="GBK"?>
<PACKET><HEAD><UUID>0</UUID><RETURNCODE>1</RETURNCODE><RETURNMESSAGE>鎴愬姛</RETURNMESSAGE></HEAD></PACKET>

后修改文件.profile添加

export LC_ALL="zh_CN.GBK"
export LANG="zh_CN.GBK"

乱码问题解决

HTTP/1.1 200 
Content-Type: text/plain;charset=GBK
Content-Length: 142
Date: Thu, 01 Aug 2019 09:37:09 GMT

<?xml version="1.0" encoding="GBK"?>
<PACKET><HEAD><UUID>0</UUID><RETURNCODE>1</RETURNCODE><RETURNMESSAGE>成功</RETURNMESSAGE></HEAD></PACKET>

问题原因看网上说的是因为window的编码格式GBK,Linux的编码格式为UTF-8所以修改Linux编码格式就解决了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值