我试图调整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();
}
感谢
伟大的答案。我没有意识到大小限制,并认为由于'IOUtilities.streamToBytes(hpc.openInputStream())'产生了问题。上传有没有类似的限制? – mrvincenzo 2012-03-17 09:57:00
我上面发布的代码使用这种方法IOUtilities.streamToBytes(hpc.openInputStream())并且仍然不起作用。所以问题出现的原因。 – 2012-03-17 13:59:19
@ahmed Shoeib当连接建立(列表中的'getResponseCode')时发生413,而不是超过下载限制。 – 2012-03-19 08:27:19