我使用HttpUrlConnection和inputstream以及bufferedInputStream和outputstream以及BufferedOutputstream从我的wampserver下载文件,并且它正常工作但是当我跟踪任务管理器和性能选项卡时,我看到我的ram从2.02 GB增加到大约3 GB:
这是我的代码:
private void downloadFile() {
try {
this.response = this.con.getInputStream();
this.bis = new BufferedInputStream(this.response, 1024 * 1024);
this.responseContentSize = this.con.getContentLength();
if (this.responseContentSize == (this.end_range - this.start_range) + 1) {
byte buffer[] = new byte[MAX_BUFFER_SIZE];
makeTmpFile();
out = new FileOutputStream(this.tmpdir + this.tmpFilename);
bos = new BufferedOutputStream(out, 1024 * 1024);
while (true) {
int read = bis.read(buffer, 0, this.MAX_BUFFER_SIZE);
if (read == -1)
break;
bos.write(buffer, 0, read);
downloadedBytes += read;
}
if (bos != null)
bos.close();
if (out != null)
out.close();
if (fpointer != null)
fpointer.close();
if (bis != null)
bis.close();
if (response != null)
response.close();
2);//finish
}
} catch (IOException e) {
sharedDownloadStatus.setCell(this.threadIndex, STATUS, 0);//error
log.setLog("func : downloadFile =>\n couldnt download file\n" + e.getMessage());
}
我在类中创建了这个func,它实现了Runnable以在另一个线程中开始它的任务