栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java.io.UTFDataFormatException: encoded string too long: 270976 bytes

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java.io.UTFDataFormatException: encoded string too long: 270976 bytes

DataOutputStream extend FilterOutputStream implements DataOutput

function: 数据输出流是应用程序以便携式方式将java基本数据类型(包含String类型)写入到输出流,然后应用程序使用数据输入流来读取数据。

需求: 上传日志报文

日志报文格式(字节):

  • 日志标识[1]
  • 保留字段[1]
  • 日志类型编码[1]
  • 日志子类型编码[1]
  • 数据包长度[4]
  • 日志内容[n]

根据需求使用输出流的包装类DataOutputStream来进行写文件,主要是很方便,都封装好了。

    
    public File toFile(String fileName) throws IOException {
        File file = new File(fileName);
        DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
        dos.writeByte(this.logIdf);
        dos.writeByte(this.reserve);
        dos.writeByte(this.logType);
        dos.writeByte(this.logSubType);
        dos.writeInt(getLength());
        if (this.content == null) {
            this.content = new JSONObject();
        }
        this.writeUTF(dos, this.content.toJSONString());
        dos.flush();
        dos.close();
        return file;
    }

在我本地的测试机上进行测试,发现没有什么问题。(content内容主要记录ssh运维过程中传输的文件名称,操作的指令等等,本地没有搞那么多,测试传了上千个文件),提交测试后就出问题了。错误日志java.io.UTFDataFormatException encoded string too long: 270976 bytes"经过排查日志发现问题就出在了this.writeUTF(dos, this.content.toJSONString());这一行代码。

源码:

 static int writeUTF(String str, DataOutput out) throws IOException {
        int strlen = str.length();
        int utflen = 0;
        int c, count = 0;

        
        for (int i = 0; i < strlen; i++) {
            c = str.charAt(i);
            if ((c >= 0x0001) && (c <= 0x007F)) {
                utflen++;
            } else if (c > 0x07FF) {
                utflen += 3;
            } else {
                utflen += 2;
            }
        }

        if (utflen > 65535)
            throw new UTFDataFormatException(
                "encoded string too long: " + utflen + " bytes");
        ...
        ...
}

可以看到使用WriteUTF(String str, DataOutput out) 进行写文件时 可写入字符串的最大字节数为 65535个,当超过65535个字节时就会出现异常。

解决方案:这里贴出我的代码
    public File toFile(String fileName) throws IOException {
        File file = new File(fileName);
        DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
        dos.writeByte(this.logIdf);
        dos.writeByte(this.reserve);
        dos.writeByte(this.logType);
        dos.writeByte(this.logSubType);
        dos.writeInt(getLength());
        if(this.content == null){
            this.content = new JSONObject();
        }
        this.writeUTF(dos, this.content.toJSONString());
        dos.flush();
        dos.close();
        return file;
    }

    private void writeUTF(DataOutputStream outputStream, String content) throws IOException {
        int strlen = content.length();
        int utflen = 0;
        int c = 0;
        int lastIndex = 0;
        int strSize = content.getBytes(StandardCharsets.UTF_8).length;
        int sumSize = 0;
        int bufferSize = 65000; //单次写入字节
        for (int i = 0; i < strlen; i++) {
            c = content.charAt(i);
            if ((c >= 0x0001) && (c <= 0x007F)) {
                utflen++;
            } else if (c > 0x07FF) {
                utflen += 3;
            } else {
                utflen += 2;
            }
            if (utflen >= bufferSize) {
                outputStream.writeUTF(content.substring(lastIndex, i));
                lastIndex = i;
                sumSize += utflen;
                utflen = 0;
            }
        }
        if(sumSize < strSize){
            outputStream.writeUTF(content.substring(lastIndex, strlen));
        }
    }

以上就是本次遇到的问题,以及解决方案。欢迎交流,共同进步。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/830579.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号