bug记录----Java NIO 报java.nio.charset.MalformedInputException: Input length = 1

请求异常报错:Java NIO 报java.nio.charset.MalformedInputException: Input length = 1 原因:项目启动时未设置默认编码,代码中又获取了系统默认编码(在windows环境下,ADT 编译器默认编码方式GBK)。 使用的类是:org.apache.commons.io中的IOUtils public static byte[] toByteArray(Reader input) throws IOException { return toByteArray(input, Charset.defaultCharset()); } 解决: 1.在代码中设置默认编码; 2.项目启动时设置环境默认编码; 方法一、修改环境变量 (变量名:JAVA_TOOL_OPTIONS变量值:-Dfile.encoding=UTF-8) 方法二、tomcat中修改:修改catalina.bat(或startup.sh),在文件头中添加:set -Dfile.encoding=utf-8 方法三、idea中添加VM options:-Dfile.encoding=utf-8

猜你喜欢

转载自www.cnblogs.com/xyddm/p/12455903.html