gpt4 book ai didi

java - 此代码的下载速度非常慢,是否有助于改进它?

转载 作者:行者123 更新时间:2023-11-29 10:05:26 25 4
gpt4 key购买 nike

在我的 Android 应用程序中,我需要下载一个大约 40 MB 的文件,我正在我的手机中测试代码,但下载速度真的很慢,我尝试从使用我的 PC 时速度很快的不同来源下载。

这是我在下载服务中使用的代码:

URLConnection conexion;
URL url;
int lenghtOfFile = 0;


try {
url = new URL("<URL>");
conexion = url.openConnection();
conexion.connect();
lenghtOfFile = conexion.getContentLength();


try {

File f = new File(Environment.getExternalStorageDirectory() + "/testing");


InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(f.getAbsolutePath());

byte data[] = new byte[1024];

long total = 0;

while ((count = input.read(data)) != -1) {
total += count;

notification.contentView.setProgressBar(R.id.status_progress, 100, (int) (total * 100 / lenghtOfFile), false);
notification.contentView.setTextViewText(R.id.status_text, Long.toString(total * 100 / lenghtOfFile));

notificationManager.notify(42, notification);
output.write(data, 0, count);
}

output.flush();
output.close();
input.close();
} catch (Exception e) {
e.getCause();
e.getMessage();
}


} catch (Exception e) {
}


}

这段代码是否可能是 dl 速度慢的原因?关于如何使下载速度更快的任何想法?

最佳答案

改变这个

byte data[] = new byte[1024];

byte data[] = new byte[4096];

正如 commonsware 所说,以较低的频率更新您的下载进度通知。

例如:在你的循环中使用一个简单的计数器变量,当它达到 10 时更新进度,然后重置它..!

关于java - 此代码的下载速度非常慢,是否有助于改进它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9556335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com