目录

【历史上最全的处理前后端二进制流】-实践_Code
欢迎关注微信公众号:数据科学与艺术

在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
        };
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.

上述代码示例中,通过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);
  });
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

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

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

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