能够确定文件的大小要下载? [英] Possible to determine size of file to be downloaded?

查看:105
本文介绍了能够确定文件的大小要下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有以下的code读取位于网络上的一个文件,并将其写入一个文件在手机上:

I currently have the following code which reads a file located on the web and writes it to a file on the phone:

InputStream inputStream = new URL(sourceFileWebAddress).openStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
FileOutputStream fileOutputStream = new FileOutputStream(targetFile);

int count;
byte buffer[] = new byte[1024];

while ((count = bufferedInputStream.read(buffer, 0, buffer.length)) != -1)
  fileOutputStream.write(buffer, 0, count);

没有人知道它是否是可能的(使用安装上方或以其他方式),以确定它们是与下载开始(以出版进度百分比给用户作为下载前进)之前要读取的字节的总数?

Does anyone know whether it is possible (using the setup above or otherwise) to determine the total number of bytes which are to be read before commencing with the download (in order to publish percentage progress to the user as the download proceeds)?

推荐答案

使用的URLConnection <在 getContentLength 方法/ P>

use the getContentLength method of URLConnection

URL url = new URL(sourceFileWebAddress);
URLConnection connection = url.openConnection();
connection.connect();

int fileLenth = connection.getContentLength();

InputStream inputStream = url.openStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

这篇关于能够确定文件的大小要下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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