java 获取下载文件大小_java – 如何从URL下载(使用http连接)检索文件的大小?

在Java项目中,使用HTTP连接下载文件时,作者希望在下载过程中显示进度条。他们已经可以下载文件,但需要在下载过程中知道文件的大小以更新进度条状态。尝试通过`outputfile.length`获取文件大小,但只得到了“1”。作者考虑其他方法,如根据WiFi速度和文件大小估算下载时间,寻求相关解决方案。
摘要由CSDN通过智能技术生成

我正在使用一个使用http连接下载文件的项目.我在下载过程中显示一个进度条状态的水平进度条.

我的功能如下所示:

.......

try {

InputStream myInput = urlconnect.getInputStream();

BufferedInputStream buffinput = new BufferedInputStream(myInput);

ByteArrayBuffer baf = new ByteArrayBuffer(capacity);

int current = 0;

while((current = buffinput.read()) != -1) {

baf.append((byte) current);

}

File outputfile = new File(createRepertory(app,0),Filename);

FileOutputStream myOutPut = new FileOutputStream(outputfile);

myOutPut.write(baf.toByteArray());

...

}

我提前知道我的文件的大小,所以我需要检索的大小在下载(在我的while块).因此,我可以确定进度条的状态.

progressBarStatus = ((int) downloadFileHttp(url,app) * 100)/sizefile;

长的downloadFileHttp(..,..)是我的函数的名字.

我已经尝试通过使用outputfile.length来检索它,但是他的值为“1”,也许这是我要下载的文件数.

Is there any way to figure it out?

更新1

I haven’t got any thread which allow me to figure this out.

Currently I have got a horizontal progress bar whch displays only 0 and 100% whitout intermediate values. I

think about another approach. If I know the rate of my wifi and the

size of the file I can determinate the time of downloading.

I know that I can retrieve the piece of information of my Wifi

connection and the size of my file to download.

Is anybody already have worked or have thread on it?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值