我試圖調整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