Idea由字节数组转为字符串时乱码问题

原料:

1.http请求方法

public static byte[] httpSend() throws Exception {
        Map<String, Object> map = new HashMap();
        OutputStream outputStream = null;
        ObjectOutputStream objOutputStream = null;
        InputStream inputStream = null;
        ObjectInputStream objinputStream = null;
        HttpURLConnection httpURLConnection = null;
        byte[] iXMLData = null;
        try {
            //初始化连接
            map = messageToMap(map, XXX, XXX, xmlData);

            httpURLConnection = initServletClient(httpURLConnection, XXX);
            outputStream = httpURLConnection.getOutputStream();
            objOutputStream = new ObjectOutputStream(outputStream);
            objOutputStream.writeObject(map);
            objOutputStream.flush();
            objOutputStream.close();
            outputStream.flush();
            outputStream.close();
            inputStream = httpURLConnection.getInputStream();
            BufferedInputStream input = null; // 输入流,用于接收请求的数据
            byte[] buffer = new byte[2048]; // 数据缓冲区
            int count = 0; // 每个缓冲区的实际数据长度
            ByteArrayOutputStream streamXML = new ByteArrayOutputStream(); // 请求数据存放对象
            try {
                input = new BufferedInputStream(inputStream);
                while ((count = input.read(buffer)) != -1) {
                    streamXML.write(buffer, 0, count);
                }
            } catch (Exception e) {
                logger.error("Http请求返回数据处理失败,", e);
            } finally {
                if (input != null) {
                    try {
                        input.close();
                    } catch (Exception f) {
                        logger.error("Http请求返回数据流关闭失败,", f);
                    }
                }
            }
            iXMLData = streamXML.toByteArray(); // 得到一个byte数组
            httpURLConnection.disconnect();
        } catch (Exception e) {
            throw new Exception("servlet调用发生异常" + e.getMessage());
        } finally {
            if (null != inputStream) {
                inputStream.close();
            }
            if (null != outputStream) {
                outputStream.close();
            }
            if (null != objinputStream) {
                objinputStream.close();
            }
            if (null != objOutputStream) {
                objOutputStream.close();
            }
            if (null != httpURLConnection) {
                httpURLConnection.disconnect();
            }
        }
        return iXMLData;
    }

2.http请求的返回数据处理方法

public static void test(){

        byte[] resultByte = null;
        try {
            resultByte = HttpClient.httpSend(XXX, XXX, data_Param_Json);
            resultByte = ZipUtils.decompress(resultByte);
            String returnStrEncode = new String(resultByte, "UTF-8");
            String returnStr = Md5Util.authcodeDecode(returnStrEncode, KEY, "UTF-8");
            log.info("返回报文:" + returnStr);
           
        } catch (Exception e) {
            log.error("异常,", e);
        }
    }

代码运行完全正常,在Eclipse中可以运行,但在Idea中报错,异常如下:

com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException: You must initialize the xml-security library correctly before you use it. Call the static method "com.sun.org.apache.xml.internal.security.Init.init();" to do that before you use any functionality from that library.
	at com.sun.org.apache.xml.internal.security.utils.Base64.decodeInternal(Base64.java:509)
	at com.sun.org.apache.xml.internal.security.utils.Base64.decode(Base64.java:485)
	at com.insure.third.yangguang.util.YGMd5UtilForJt.authcode(YGMd5UtilForJt.java:327)
	at com.insure.third.yangguang.util.YGMd5UtilForJt.authcodeDecode(YGMd5UtilForJt.java:364)
	at com.insure.third.yangguang.service.YGPolicyServiceApi.applyPolicy(YGPolicyServiceApi.java:70)
	at com.insure.third.yangguang.YGInsurePolicyTest.validateTest(YGInsurePolicyTest.java:33)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

其实和这个完全没有关系,其实是Idea中设置的Project Encoding导致的,

此处的编码方式需要和要编译成的字符串的编码格式一致。

修改成“UTF-8”即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值