使用下面的代码,我可以从url加载任何一个字节的文件,但这种方式对不超过256kb的文件很好。
所以我想要另一种方法来加载更大的文件为字节而不使用BlackBerry Combiner
我的代码:
HttpConnection hpc = null;
try {
hpc = (HttpConnection) Connector.open(url
+ ConnectionManager.getTimeOut(5000)
+ ConnectionManager.updateConnectionSuffix());
hpc.setRequestMethod(HttpConnection.GET);
hpc.setRequestProperty("Connection", "Keep-Alive");
if (hpc.getResponseCode() != 200) {
return null;
}
byte[] data = IOUtilities.streamToBytes(hpc.openInputStream());
hpc.close();
return data;
} catch (Exception e) {
return null;
}发布于 2012-03-19 17:20:32
我尝试将BlackBerry Combiner调整为在下载大文件为字节时使用。下面列出的代码对我来说很好。
-呼叫
byte[] data = downloadLargeFiles(url);
if (data != null) {
invoke(data.length + " ");
Bitmap bitmap = Bitmap.createBitmapFromBytes(data, 0,
data.length, 1);
manager.add(new BitmapField(bitmap));
}-函数
public byte[] downloadLargeFiles(String url) throws Exception {
int chunkIndex = 0;
int totalSize = 0;
String currentFile = url + ConnectionManager.getTimeOut(5000)
+ ConnectionManager.updateConnectionSuffix();
HttpConnection conn;
InputStream in;
int rangeStart = 0;
int rangeEnd = 0;
int chunksize = 100000;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while (true) {
conn = (HttpConnection) Connector.open(currentFile,
Connector.READ_WRITE, true);
rangeStart = chunkIndex * chunksize;
rangeEnd = rangeStart + chunksize - 1;
conn.setRequestProperty("Range", "bytes=" + rangeStart + "-"
+ rangeEnd);
int responseCode = conn.getResponseCode();
if (responseCode != 200 && responseCode != 206) {
// Dialog.alert("End "+responseCode);
break;
}
in = conn.openInputStream();
int length = -1;
byte[] readBlock = new byte[256];
int fileSize = 0;
while ((length = in.read(readBlock)) != -1) {
bos.write(readBlock, 0, length);
fileSize += length;
Thread.yield(); // Try not to get cut off
}
totalSize += fileSize;
chunkIndex++; // index (range) increase
in.close();
conn.close();
in = null;
conn = null;
Thread.sleep(1000);
}
bos.close();
return bos.toByteArray();
}谢谢
发布于 2012-03-16 20:12:06
该限制是由作为BlackBerry设备和互联网之间的代理的MDS施加的。BIS和BES服务器都是MDS。因此,在不消除限制的情况下,您必须拆分您的下载以适应您最大的数据大小。HTTP协议已经通过Range请求报头支持此功能,如DownloadCombiner示例所示,不需要创建自己的机制。这是保证下载任意大小的文件的唯一方法。
也就是说,根据您的情况,有一些方法可以消除/绕过该限制:
\MDS\config\rimpublic.property
找到IPPP.connection.MaxNumberOfKBytesToSend行并增加它以满足您的需要。这不是一个好的实践,因为您的应用程序将在模拟器中运行良好,但在真实设备上将失败。总是希望你的模拟器行为尽可能地接近真实设备。
https://stackoverflow.com/questions/9720455
复制相似问题