美文网首页
HttpClient实现上传下载进度显示

HttpClient实现上传下载进度显示

作者: Frank_Kivi | 来源:发表于2018-09-17 14:57 被阅读106次

直接上代码了。

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE) {
                @Override
                public void writeTo(OutputStream outstream) throws IOException {
                    super.writeTo(new FilterOutputStream(outstream) {
                        @Override
                        public void write(@NonNull byte[] buffer, int offset, int length) throws IOException {
                            super.write(buffer, offset, length);
                           
                        }
                    });
                }
            };

这个是上传的进度监听。注意要使用FilterOutputStream

                    long total = response.getEntity().getContentLength();
                    float current = 0;
                    while ((read_byte = file_in_stream.read(tmp_buffer)) > 0) {
                        file_out_stream.write(tmp_buffer, 0, read_byte);
                        if (total > 0) {
                            current += read_byte;
                        }
                    }

相关文章

网友评论

      本文标题:HttpClient实现上传下载进度显示

      本文链接:https://www.haomeiwen.com/subject/zbplnftx.html