解析字节文件

package com.aigyun.util;

import com.aigyun.constant.BusinessException;
import org.springframework.web.multipart.MultipartFile;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.*;

/**
 * Created by Administrator on 2019/6/3.
 */
public class IoUtil {
    /**
     * 分析字节流转换为map
     * @param file
     * @return
     */
    public static List<Map> buileFlyRealTimeData(MultipartFile file) {

        List<Map> flyRealTimeData = new ArrayList<>();
        DataInputStream in = null;
        try {
            in = new DataInputStream(file.getInputStream());
            int length = in.available();
            if (length <= 0) {
                return flyRealTimeData;
            }

            //索引读取
            char index = (char)in.readByte();

            //读取头信息
            byte header [] = new byte[84];
            in.read(header,0,84);
            //机型注册号
            String uav_no = new String(Arrays.copyOfRange(header,1,20)).trim();
            //任务
            String order_no = new String(Arrays.copyOfRange(header,21,40)).trim();
            //版本
            String app_ver_no = new String(Arrays.copyOfRange(header,41,60)).trim();
            //序列
            String flight_control = new String(Arrays.copyOfRange(header,61,84)).trim();

            int n = 1 + (length -85) / 74;  //循环次数

            for (int i=0; i< n;i++){
                Map realTimeEntity = new HashMap();
                realTimeEntity.put("uav_no", uav_no);
                realTimeEntity.put("order_no", order_no);
                realTimeEntity.put("app_ver_no", app_ver_no);
                realTimeEntity.put("flight_control", flight_control);
                System.out.println("-----------------------===========第"+i+"行");
                if (i == 522) {
                    ;
                }
                //架次
                realTimeEntity.put("flight_seq", in.readInt());
                //时间戳
                realTimeEntity.put("now_time", in.readInt());
                //状态
                realTimeEntity.put("state", in.readByte());
                //飞行时间
                realTimeEntity.put("flight_time", in.readLong());
                //经度
                realTimeEntity.put("longitude", in.readInt());
                //纬度
                realTimeEntity.put("latitude", in.readInt());
                //相对高度
                realTimeEntity.put("height", in.readInt());
                //绝对高度
                realTimeEntity.put("altitude", in.readInt());
                //偏航角
                realTimeEntity.put("path_angle", (short)in.readChar());
                //仰俯角
                realTimeEntity.put("pitch_angle", (short)in.readChar());
                //横滚角
                realTimeEntity.put("roll_angle", (short)in.readChar());
                //地速
                realTimeEntity.put("horizontal_velocity", in.readInt());
                //喷嘴是否工作
                realTimeEntity.put("is_nozzle_work", in.readByte());
                //喷嘴口径
                realTimeEntity.put("nozzle_diameter", (short)in.readChar());
                //喷嘴角度
                realTimeEntity.put("nozzle_angle", (short)in.readChar());
                //喷嘴压力
                realTimeEntity.put("nozzle_pressure", (short)in.readChar());
                //喷幅
                realTimeEntity.put("spray_range", in.readInt());
                //剩余药量
                realTimeEntity.put("remain_dose", in.readInt());
                //使用药量
                realTimeEntity.put("used_dose", in.readInt());
                //喷洒流速
                realTimeEntity.put("cur_flow", in.readInt());
                //喷洒面积
                realTimeEntity.put("flight_area", in.readInt());
                //飞行距离
                realTimeEntity.put("flight_length_section", in.readInt());
                flyRealTimeData.add(realTimeEntity);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException(200000,"上传失败," + e.getMessage());
        }finally {
            try {
                in.close();
            } catch (IOException ioe){
                ioe.printStackTrace();
                throw new BusinessException(200000,"上传失败," + ioe.getMessage());
            }
        }
        return flyRealTimeData;
    }
}

转载于:https://my.oschina.net/u/1763091/blog/3058156

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值