获取 ObjectOutputStream/ObjectInputStream 的进度 [英] Getting Progress of ObjectOutputStream/ObjectInputStream

查看:97
本文介绍了获取 ObjectOutputStream/ObjectInputStream 的进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近想出了如何使用 ObjectOutputStream 和 ObjectInputStream 通过服务器和客户端之间的简单 Java 套接字连接发送对象.我想知道我是否想传输一个可能很大的对象,例如一个图像,是否可以放置一个线程来跟踪已发送/接收多少数据的进度?如果这个问题的答案不是很直接,有人可以解释我如何去做类似的事情吗?提前致谢!

I recently figured out how to use ObjectOutputStream and ObjectInputStream to send objects over a simple Java socket connection between a server and a client. I was wondering if I wanted to transfer an object that might be large in size, for example an image, is it possible to put a thread that keeps track of the progress of how much data has been sent/received? If the answer to this question isn't very direct, could someone explain how I might go about doing something similar? Thanks in advance!

推荐答案

Apache Commons IO 库有一对类 CountingInputStreamCountingOutputStream 实现字节计数输入/输出流过滤器.

The Apache Commons IO library has a pair of classes CountingInputStream and CountingOutputStream that implement byte counting input/output stream filters.

如果您将这些插入到您的流链中,您可以跟踪读取或写入的字节数.(这些过滤器必须插入物理输入/输出流和对象流之间的某个位置.)

If you insert these into your stream chains you can track the number of bytes read or written. (These filters have to be inserted somewhere between the physical input/output stream and object stream.)

您可以通过继承 FilterInputStreamFilterOutputStream 类.甚至还有一个名为 ProgressMonitorInputStream可能实现您所需要的.

You could implement the same thing yourself by subclassing the FilterInputStream and FilterOutputStream classes. And there is even a Swing class called ProgressMonitorInputStream that might implement exactly what you need.

这篇关于获取 ObjectOutputStream/ObjectInputStream 的进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆