我試圖顯示OutputStreamWriter
已經發送的數據的大小,但似乎write
方法有點像異步,這意味着如果文件是60M,上傳速率是200K/s,輸出只顯示一行「數據發送:61210K「(或任何大數字),而不是應該是什麼(每秒小數)如何監視HttpURLConnection發送的數據的大小?
我錯過了什麼嗎?
碼片:
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();
OutputStreamWriter包裝了什麼'out'?一個底層的'StreamEncoder'會寫入。 –
它只是由java.net.URLConnection.getOutputStream()返回的OutputStream – NSF