问题描述
我有一个应用程序可以使用Internet连接来下载数据。我使用的HttpURLConnection
做到这一点。
问题:我的应用程序水渠网络带宽,因此用户会慢慢地浏览他们的浏览器。我希望让他们任意设置本身的带宽限制,而不是像这个网站。我已经知道这件事。
问:如何设置带宽限制,同时下载?例如:500 KB / s(每秒千字节)
。下面是我的方法下载文件:
//这是状态codeS。
公共静态最终诠释下装= 0;
公共静态最终诠释PAUSED = 1;
完善的公共静态最终诠释= 2;
公共静态最终诠释已取消= 3;
公共静态最终诠释ERROR = 4;
私人长时间下载;
私人诠释状态;
私人无效downloadFile(字符串requestUrl)抛出IOException异常{
的InputStream流= NULL;
RandomAccessFile的输出= NULL;
状态=下载;
下载= 0;
网址URL =新的URL(requestUrl);
尝试 {
System.setProperty(http.keepAlive,假);
输出=新的RandomAccessFile(my_directory,RW);
//以URL打开连接。
HttpURLConnection的连接=(HttpURLConnection类)url.openConnection();
connection.setConnectTimeout(5000);
connection.connect();
//确保响应code是在200范围内。
INT状态code = connection.getResponse code();
如果(状态code!= 200){
状态=错误;
}
流= connection.getInputStream();
而(状态==下载){
字节的缓冲区[] =新的字节[1024];
//从服务器读入缓冲区。
INT读= stream.read(缓冲区);
如果(阅读== -1)
打破;
//写入缓冲区文件。
output.write(缓冲,0,读);
下载+ =读取;
}
状态==齐全;
}赶上(例外五){
状态=错误;
} 最后 {
如果(输出!= NULL){
尝试 {
output.close();
}赶上(例外五){}
}
//关闭连接到服务器。
如果(流!= NULL){
尝试 {
stream.close();
}赶上(例外五){}
}
}
}
如果你有一个插槽
你可以只设置接收缓冲区大小为所需的带宽时延产品,但是,你不这样做,你就只能睡之间接听电话。实验或一些基于反馈的运算将产生适当的睡眠间隔。
I have an app that can use internet connection to downloading data. I'm using HttpURLConnection
to do that.
Problem: My app drains the internet bandwidth, so users will browsing slowly on their Browser. I want to make them optionally to set the bandwidth limit by itself, not like this site. I have knew about it.
Question: How to set the bandwidth limit while downloading? For example: 500 KB/s (KiloBytes per second).
Here's my method to download a file:
// These are the status codes.
public static final int DOWNLOADING = 0;
public static final int PAUSED = 1;
public static final int COMPLETE = 2;
public static final int CANCELLED = 3;
public static final int ERROR = 4;
private long downloaded;
private int status;
private void downloadFile(String requestUrl) throws IOException {
InputStream stream = null;
RandomAccessFile output = null;
status = DOWNLOADING;
downloaded = 0;
URL url = new URL(requestUrl);
try {
System.setProperty("http.keepAlive", "false");
output = new RandomAccessFile(my_directory, "rw");
// Open connection to URL.
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.connect();
// Make sure response code is in the 200 range.
int statusCode = connection.getResponseCode();
if (statusCode != 200) {
status = ERROR;
}
stream = connection.getInputStream();
while (status == DOWNLOADING) {
byte buffer[] = new byte[1024];
// Read from server into buffer.
int read = stream.read(buffer);
if (read == -1)
break;
// Write buffer to file.
output.write(buffer, 0, read);
downloaded += read;
}
status == COMPLETE;
} catch (Exception e) {
status = ERROR;
} finally {
if (output != null) {
try {
output.close();
} catch (Exception e) {}
}
// Close connection to server.
if (stream != null) {
try {
stream.close();
} catch (Exception e) {}
}
}
}
If you had a Socket
you could just set the receive buffer size to the desired bandwidth-delay product, but, as you don't, you will just have to sleep between receive calls. Experiment or some feedback-based arithmetic will yield the appropriate sleep intervals.
这篇关于限制带宽,同时下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!