Java: resume download in urlconnection
•
Java
I wrote a program to download some files from some servers
.... File fcheck=new File(SaveDir+"/"+filename); if(resume@R_138_2419@.isSelected() && fcheck.exists()){ connection.setRequestProperty("Range","Bytes="+(fcheck.length())+"-"); } connection.setDoInput(true); connection.setDoOutput(true); BufferedInputStream in = new BufferedInputStream (connection.getInputStream()); pbar.setIndeterminate(false); pbar.setStringPainted(true); java.io.FileOutputStream fos ; if(resume@R_138_2419@.isSelected()){ if(fcheck.exists()){ if(connection.getHeaderField("Accept-Ranges").equals("bytes")){ fos = new java.io.FileOutputStream(SaveDir+"/"+filename,true); }else{ fos = new java.io.FileOutputStream(SaveDir+"/"+filename); } }else{ fos = new java.io.FileOutputStream(SaveDir+"/"+filename); } }else{ fos = new java.io.FileOutputStream(SaveDir+"/"+filename); } ....@H_419_6@我正在我知道支持的服务器上进行测试.
我下载了一些字节(72720)
然后尝试恢复它.
然后我用一个十六进制编辑器打开文件,在偏移72720,第一个字节被重复:
字节0-36:FLV ………….«………. onMetaData
字节72720-72756:FLV ………….«………. onMetaData
它从开始开始下载!
当我通过wget做它正确和Content-Range字段的响应!
wget日志中的“302 FOUND”和“206 Partial Content”的服务器响应.
“302 FOUND”能否引起问题?问题是什么 ?
谢谢.
Solution
Try:
connection. setRequestProperty(“Range”,“bytes =”fcheck.length()“ – ”);
Lower case range specifier according to specification In addition, if some of your files are 500 bytes, your byte range is 0-499, and you want 500
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码