gpt4 book ai didi

java - SetRequestProperty 有效,但下载完整大小

转载 作者:行者123 更新时间:2023-12-02 02:47:48 28 4
gpt4 key购买 nike

尝试使用具有暂停/恢复功能的“输入/输出流”进行下载。问题是当我将requestproperty设置为我下载的数量时:connection.setRequestProperty(“Range”,“bytes =”+downloaded+“-”);并读取长度:int fileLength = connection.getContentLength();它给了我正确的数量,但在下载代码(while 循环)中,即使我设置了正确的请求,它也会下载文件的完整大小。

示例:它开始正常下载

total:2048      data length:1570024
07-21 11:26:17.650 D: zz path:v_3_1 total:4096 file length(should be full):1570024
07-21 11:26:17.650 D: zz path:v_3_1 total:6144 file length(should be full):1570024
07-21 11:26:17.651 D: zz path:v_3_1 total:8192 file length(should be full):1570024
07-21 11:26:17.652 D: zz path:v_3_1 total:10240 file length(should be full):1570024
07-21 11:26:17.652 D: zz path:v_3_1 total:12288 file length(should be full):1570024
07-21 11:26:17.653 D: zz path:v_3_1 total:14336 file length(should be full):1570024
07-21 11:26:17.653 D: zz path:v_3_1 total:16384 file length(should be full):1570024

在这里我暂停然后恢复它(工作正常):

     data length is:830696
07-21 11:26:30.949 D: zz path:v_3_1 total:741376 file length(should be full):1570024
07-21 11:26:30.950 D: zz path:v_3_1 total:743424 file length(should be full):1570024
07-21 11:26:30.950 D: zz path:v_3_1 total:745472 file length(should be full):1570024
07-21 11:26:30.950 D: zz path:v_3_1 total:747520 file length(should be full):1570024
07-21 11:26:30.950 D: zz path:v_3_1 total:749568 file length(should be full):1570024
07-21 11:26:30.950 D: zz path:v_3_1 total:751616 file length(should be full):1570024
07-21 11:26:30.951 D: zz path:v_3_1 total:753664 file length(should be full):1570024
07-21 11:26:30.951 D: zz path:v_3_1 total:755712 file length(should be full):1570024
07-21 11:26:31.070 D: zz path:v_3_1 total:757760 file length(should be full):1570024
07-21 11:26:31.071 D: zz path:v_3_1 total:759808 file length(should be full):1570024
07-21 11:26:31.074 D: zz path:v_3_1 total:761856 file length(should be full):1570024
07-21 11:26:31.076 D: zz path:v_3_1 total:763904 file length(should be full):1570024
07-21 11:26:31.077 D: zz path:v_3_1 total:765952 file length(should be full):1570024
07-21 11:26:31.078 D: zz path:v_3_1 total:768000 file length(should be full):1570024

但随后它会超过文件的完整大小并停止在 1570024(完整大小)+739328(我暂停之前的大小)...它应该只会上升到 1570024 并停止。

07-21 11:26:32.125 D: zz    path:v_3_1      total:1750816      file length(should be full):1570024

代码:

 try {
URL url = new URL(sUrl[1]);
URLConnection connection = url.openConnection();

if(resume){downloaded=viv.getIsdown2(path);
connection.setRequestProperty("Range", "bytes=" + downloaded + "-");}
connection.setDoInput(true);
connection.setDoOutput(true);
connection.connect();
int fileLength = connection.getContentLength();
Log.d("dt", "zz "+path+" data length is:" + fileLength);
fos = new FileOutputStream(outfile);
fis = new FileInputStream(outfile);
encipher = Cipher.getInstance("AES");
encipher.init(Cipher.ENCRYPT_MODE, secretKey);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(outfile);
cis = new CipherInputStream(fis, encipher);
byte data[] = new byte[8 * 1024];
total = downloaded;
// total = 0;
fileLength+=downloaded;
int count;
while ((count = input.read(data)) !=-1) {

total += count;
progpercent = (int) (total * 100 / fileLength);
publishProgress(progpercent, total); Log.d("dt", "zz path:"+path+" total:" + total+" file length(should be full):" + fileLength);
output.write(data, 0, count);
if(isCancelled())break;


}
output.flush();
output.close();
input.close();

更新:既然我弄清楚了我的主要问题的答案是什么,另一个问题是,即使在 fileoutputStream 中使用append=true,写入的文件也会覆盖当前的文件,我不知道为什么......有什么建议吗?

最佳答案

在暂停下载之前跟踪已下载的大小,并在恢复功能中,您可以确保从总量中减去暂停之前已下载的量,以获得剩余下载量。

connection.setRequestProperty("Range", "bytes=" + (total - downloaded) + "-");}

关于java - SetRequestProperty 有效,但下载完整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57132872/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com