我试图显示已经发送的数据的大小,OutputStreamWriter
但似乎该write
方法有点像异步,这意味着如果文件为 60M,上传速率为 200K/s,输出仅显示一行“数据已发送:61210K "(或任何大的数字)而不是应该的(每秒的小数字)
我错过了什么?
代码段:
Writer writer = new OutputStreamWriter(out, POST_ENCODING);
char[] buf = new char[1024];
int read = 0;
long bytes = 0;
while ((read = reader.read(buf)) >= 0) {
bytes += read;
if (System.currentTimeMillis() - lastMsgTimeStamp > 1000) {
lastMsgTimeStamp = System.currentTimeMillis();
System.out.println("Data sent: " + (bytes / 1024) + " K");
}
writer.write(buf, 0, read);
}
writer.flush();