这是我的代码,当我运行它时,它正确下载文件但在may pc中使用了很多ram。例如:当我运行应用程序时,我的RAM是2.02 GB我的RAM高达3GB并且减慢了我的电脑。 我使用wampserver作为我的网络服务器,并使用我的代码并将http标头设置为可能的对象,我正确加载文件,感谢您的帮助。
private void downloadFile() {
try {
this.response = this.con.getInputStream();
this.bis = new BufferedInputStream(this.response, 1024 * 1024);
//int responseCode = this.con.getResponseCode();
this.responseContentSize = this.con.getContentLength();
//check kon bebin meghadare bargashti content ba on range ke to behesh gofti mosavi hast ya kheir
if (this.responseContentSize == (this.end_range - this.start_range) + 1) {
//fpointer = new RandomAccessFile(this.filePath, "rw");
//fpointer.seek(this.start_range);
byte buffer[] = new byte[MAX_BUFFER_SIZE];
//out = new FileOutputStream(this.filePath);
makeTmpFile();
out = new FileOutputStream(this.tmpdir + this.tmpFilename);
bos = new BufferedOutputStream(out, 1024 * 1024);
sharedDownloadStatus.setCell(this.threadIndex, STATUS, 1);//downloading
while (true) {
//int read = response.read(buffer, 0, this.MAX_BUFFER_SIZE);
int read = bis.read(buffer, 0, this.MAX_BUFFER_SIZE);
if (read == -1)
break;
//out.write(buffer, 0, read);
//fpointer.write(buffer, 0, read);
bos.write(buffer, 0, read);
downloadedBytes += read;
sharedDownloadStatus.setCell(this.threadIndex, DOWNLOADED, downloadedBytes);
}
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();
sharedDownloadStatus.setCell(this.threadIndex, STATUS, 2);//finish
}
} catch (IOException e) {
sharedDownloadStatus.setCell(this.threadIndex, STATUS, 0);//error
log.setLog("func : downloadFile =>\n couldnt download file\n" + e.getMessage());
}
}
答案 0 :(得分:1)
您不需要兆字节缓冲区,但主要问题是HttpURLConnection将缓冲所有内容,除非有效的固定长度或分块传输模式由服务器控制,而不是客户端。