简体   繁体   English

DataOutputStream写入进度不准确

[英]DataOutputStream write progress not accurate

DataOutputStream wr = new DataOutputStream(
                    urlConnection.getOutputStream());
            fis = new FileInputStream(image_path);
            int total = 0;
            int bytes_read = 0;
            int buffer_size = 1024;
            byte[] buffer = new byte[buffer_size];
            bytes_read = fis.read(buffer, 0, buffer_size);
            int progress = 0;
            while (bytes_read > 0) {
                total += bytes_read;
                wr.write(buffer, 0, bytes_read);
                int bytes_available = fis.available();
                progress = (int) ((total * 100) / bytes.length);
                Log.i("asd", "direct progress: " + progress);
                buffer_size = Math.min(bytes_available, buffer_size);
                buffer = new byte[buffer_size];
                bytes_read = fis.read(buffer, 0, buffer_size);
            }
            Log.i("asd", "data written to target so far is: " + wr.size()+ " file length: "+bytes.length);
            wr.flush();
            wr.close();

Uploads the file completely onto server. 将文件完全上传到服务器。 But my intention is to get the progress of already uploaded bytes. 但是我的意图是获取已经上传的字节的进度。 I can get that also(which I think is inaccurate). 我也可以得到(我认为这是不准确的)。 When i run this code, It shows me progress from 0 to 100 but the file gets uploaded to server after 1 minute or so. 当我运行此代码时,它显示了从0到100的进度,但是文件在1分钟左右后被上传到服务器。

I have tried with files less than 1MB and the progress is accurate. 我尝试使用的文件小于1MB,并且进度准确。 But when the file is larger, I get the inaccuracy of progress problem. 但是,当文件较大时,我会遇到进度不准确的问题。

Your progress indicator is inaccurate because you're dividing (bytes read) by (bytes read in one read). 您的进度指示器不准确,因为您将(读取的字节数)除以(一次读取的字节数)。 It means that you're displaying number of reads as upload percentage which is incorrect and works only for files uploaded in 100 reads/writes. 这意味着您将读取次数显示为上载百分比,这是不正确的,并且仅适用于100次读取/写入中上载的文件。

You should read file length once at the beginning. 您应该在开始时读取一次文件长度。 Then accumulate total bytes read/written. 然后累积读取/写入的总字节数。 Then progress equals: 然后进度等于:

progress = 100 * totalBytesWritten / fileLength 进度= 100 *写入的总字节数/文件长度

DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());
fis = new FileInputStream(image_path);
int fileLength = new File(image_path).length();

int bytes_read = 0;
int bytesReadTotal = 0;
int buffer_size = 1024;
byte[] buffer = new byte[buffer_size];
int progress = 0;

while ((bytes_read = fis.read(buffer, 0, buffer_size)) > 0) {
     wr.write(buffer, 0, bytes_read);
     bytesReadTotal += bytes_read;
     progress = (int) (100.0f * bytesReadTotal / fileLength);
     Log.i("asd", "direct progress: " + progress);
}
Log.i("asd", "data written to target so far is: " + wr.size()+ " file length: "+bytes.length);
wr.flush();
wr.close();

You also don't have to recreate buffer each time you'd like to read/write something. 您也不必每次想读/写东西时都重新创建缓冲区。 You can reuse the old one. 您可以重用旧的。

connection.setFixedLengthStreamingMode((int) requestLength);

会成功的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM