在Java中如何接收通过C#代码发送的文件流

byte[] data = File.ReadAllBytes(Utils.MrFile.PathName);
pdfFile.data = BitConverter.ToString(data);

通过以上c#代码,发送流,在java中如何接收?

// 获取从C#发送的十六进制字节数组字符串
String hexData = "AB-CD-EF-01-23";

// 移除连字符,得到十六进制字节数组的字符串数组
String[] byteStrings = hexData.split("-");

// 创建一个字节数组来存储解析后的数据
byte[] data = new byte[byteStrings.length];

// 将每个字节的字符串转换为字节值
for (int i = 0; i < byteStrings.length; i++) {
    // 解析为十六进制字节值
    byte b = (byte) Integer.parseInt(byteStrings[i], 16);
    data[i] = b;
}

// 现在你可以使用字节数组 'data' 进行进一步处理

请注意,上述代码仅演示了如何将由 BitConverter.ToString() 方法转换的十六进制字节数组字符串还原为字节数组。实际应用中,你可能需要根据具体的网络通信协议进行数据传输和接收。

通过字节数组转文件:

public static File bytesToFile(byte[] bytes, String outPath, String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(outPath);
            if (!dir.exists() && dir.isDirectory()) { //判断文件目录是否存在
                dir.mkdirs();
            }
            file = new File(outPath + File.separator + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return file;
	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值