使用restTemplate作为请求时,内部添加拦截器处理响应体的解码,返回一个新的ByteArrayInputStream(repsonse.getBytes())将json解析成对象,而ide中的代码运行时没有问题。用bat脚本在win10上打包运行后,报JSONparseerror:InvalidUTF-8startbyte错误。猜测是没有用utf-8在windows上运行。检查getBytes()的源代码。不带参数构造时,会先使用defaultCharset()查找所用文件的编码方式。如果没有找到,就会使用utf-8编码。所以getBytes()在使用bat脚本运行时不使用utf-8编码。publicstaticCharsetdefaultCharset(){if(defaultCharset==null){synchronized(Charset.class){Stringcsn=AccessController.doPrivileged(newGetPropertyAction("file.encoding"));字符集cs=lookup(csn);if(cs!=null)defaultCharset=cs;否则defaultCharset=forName("UTF-8");}}返回默认字符集;}使用getBytes(StandardCharsets.UTF-8)解决问题。
