【历史上最全的处理前后端二进制流】-实践

在这里插入图片描述
欢迎关注微信公众号:数据科学与艺术 作者WX:superhe199

在Java中,可以使用java.io包中的InputStream类来处理二进制流。

后端接口

下面是一个示例的Java代码,用于创建一个二进制流的接口,流中包含设备ID和Code。在Vue前端解析数据时,还会进行空指针判断。

Java代码:

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

public class BinaryStreamAPI {

    public static void main(String[] args) {
        // 模拟获取二进制流数据
        byte[] binaryStreamData = getBinaryStreamData();
        
        // 解析二进制流数据
        parseBinaryStreamData(binaryStreamData);
    }

    public static byte[] getBinaryStreamData() {
        // 模拟生成二进制流数据,其中包括设备ID和Code
        String deviceId = "ABC123";
        int code = 12345;
        
        byte[] deviceIdBytes = deviceId.getBytes();
        byte[] codeBytes = intToByteArray(code);
        
        byte[] binaryStreamData = new byte[deviceIdBytes.length + codeBytes.length];
        
        System.arraycopy(deviceIdBytes, 0, binaryStreamData, 0, deviceIdBytes.length);
        System.arraycopy(codeBytes, 0, binaryStreamData, deviceIdBytes.length, codeBytes.length);
        
        return binaryStreamData;
    }
    
    public static void parseBinaryStreamData(byte[] binaryStreamData) {
        // 解析二进制流数据中的设备ID和Code
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(binaryStreamData);
        DataInputStream dataInputStream = new DataInputStream(byteArrayInputStream);

        try {
            // 读取设备ID
            byte[] deviceIdBytes = new byte[6];
            dataInputStream.read(deviceIdBytes);
            String deviceId = new String(deviceIdBytes);
            
            // 读取Code
            int code = dataInputStream.readInt();
            
            // 进行空指针判断
            if(deviceId != null && !deviceId.isEmpty()) {
                System.out.println("设备ID:" + deviceId);
            } else {
                System.out.println("设备ID为空或不存在");
            }
            
            System.out.println("Code:" + code);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static byte[] intToByteArray(int value) {
        return new byte[] {
                (byte)(value >>> 24),
                (byte)(value >>> 16),
                (byte)(value >>> 8),
                (byte)value
        };
    }
}

上述代码示例中,通过getBinaryStreamData()方法生成模拟的二进制流数据,其中包括设备ID和Code。然后,在parseBinaryStreamData()方法中解析二进制流数据,读取设备ID和Code,并进行空指针判断。

Vue前端

使用XMLHttpRequest或者fetch来接收二进制流的接口数据。一种常见的处理方式是使用ArrayBuffer和DataView来解析二进制数据,然后提取设备ID和Code。

以下是一个简单的示例代码:

// 发起请求获取二进制数据
fetch('your-api-url', {
  method: 'GET',
  responseType: 'arraybuffer'
})
  .then(response => response.arrayBuffer())
  .then(data => {
    // 创建一个DataView来解析二进制数据
    const view = new DataView(data);

    // 获取设备ID和Code
    const deviceId = view.getInt32(0);  // 假设设备ID的偏移量为0,数据类型为Int32
    const code = view.getUint16(4);     // 假设Code的偏移量为4,数据类型为Uint16

    // 使用设备ID和Code进行后续操作
    console.log('设备ID:', deviceId);
    console.log('Code:', code);
  })
  .catch(error => {
    console.error('请求接口失败:', error);
  });

在上述代码中,通过fetch发送GET请求获取二进制数据,并将responseType设置为arraybuffer,以便接收二进制数据。然后使用arrayBuffer()方法将response转换为ArrayBuffer对象。

接下来,利用DataView创建一个视图来解析ArrayBuffer中的二进制数据。根据二进制数据的结构,使用getInt32和getUint16等方法来提取设备ID和Code。getInt32和getUint16方法的参数表示偏移量,即数据在ArrayBuffer中的位置。

最后,可以使用解析得到的设备ID和Code进行后续操作。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贺公子之数据科学与艺术

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值