将文件转化为数据流,对文件进行下载

将文件转化为数据流,对文件进行下载

/**
*将文件按照流的形式读取
*/
public static void writeFileToStream(FileInputStream fileInputStream, OutputStream outputStream, Long size) throws IOException {
        FileChannel fileChannel = null;
        WritableByteChannel writableByteChannel = null;
        try {
            fileChannel = fileInputStream.getChannel();
            writableByteChannel = Channels.newChannel(outputStream);
            fileChannel.transferTo(0, size, writableByteChannel);
            outputStream.flush();
        } catch (Exception e) {

        } finally {
            fileInputStream.close();
            outputStream.close();
            fileChannel.close();
            writableByteChannel.close();
        }
    }

/**
* 文件下载预览
*/
    @GetMapping(value = "/previewTest/{filename}")
    public void preview(@PathVariable("filename") String filename, HttpServletResponse response) throws IOException {
        String filePath = path +filename;//文件的访问路径
        File file = new File(filePath);//对文件进行读取
        FileProcess.writeFileToStream(new FileInputStream(filePath), response.getOutputStream(), file.length());//进行文件流的获取
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
将proto文件换为数据流需要使用protocol buffer编译器。protocol buffer是一种序列化数据结构的协议,可以将结构化数据序列化为二进制格式,从而方便传输和存储。下面是将proto文件换为数据流的步骤: 1. 定义proto文件,描述数据结构和消息格式。 2. 使用protocol buffer编译器将proto文件编译成目标语言的代码。 3. 在代码中使用protocol buffer提供的API,将数据结构换为消息对象。 4. 调用消息对象的序列化方法,将消息对象换为二进制格式的数据流。 以下是一个简单的Java代码示例,演示将proto文件换为数据流: ```java // 导入protocol buffer库 import com.google.protobuf.ByteString; import com.google.protobuf.InvalidProtocolBufferException; import com.example.proto.MyMessageProto.MyMessage; // 读取proto文件 InputStream input = new FileInputStream("my_message.proto"); // 编译proto文件 Descriptors.FileDescriptor fileDescriptor = Descriptors.FileDescriptor.buildFrom(input, new Descriptors.FileDescriptor[0]); Descriptors.Descriptor messageDescriptor = fileDescriptor.findMessageTypeByName("MyMessage"); DynamicMessage.Builder messageBuilder = DynamicMessage.newBuilder(messageDescriptor); // 构造消息对象 messageBuilder.setField(messageDescriptor.findFieldByName("id"), 123); messageBuilder.setField(messageDescriptor.findFieldByName("name"), "John"); messageBuilder.setField(messageDescriptor.findFieldByName("email"), "john@example.com"); DynamicMessage message = messageBuilder.build(); // 序列化消息对象 ByteString bytes = message.toByteString(); // 输出二进制数据流 System.out.println(Arrays.toString(bytes.toByteArray())); ``` 在上面的代码中,我们使用了Google提供的protocol buffer库,读取了一个名为my_message.proto的proto文件,并编译成Java代码。然后,我们构造了一个消息对象,并调用其toByteString()方法将消息对象序列化为二进制数据流。最后,我们输出了二进制数据流的字节数组。 需要注意的是,在使用protocol buffer进行数据传输时,发送方和接收方必须使用相同的proto文件和消息格式,否则无法正确解析数据流

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值