首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

断点续传时先测出已上载文件的大小,再用InputStream的skip()跳转可以吗

2012-10-26 
断点续传时先测出已下载文件的大小,再用InputStream的skip()跳转可以吗?如题,为什么我的暂停后又从头开始

断点续传时先测出已下载文件的大小,再用InputStream的skip()跳转可以吗?
如题,为什么我的暂停后又从头开始下然后追加在后面?

[解决办法]
InputStream的skip()方法有缺陷,可以换用这个方法:

Java code
public static InputStream skipFully(InputStream in,long howMany)throws Exception{        long remainning = howMany;        long len = 0;        while(remainning>0){            len = in.skip(len);            remainning -= len;        }        return in;    }
[解决办法]
Java code
Thread dl = new Thread(){                    public void run(){                        try{                            String n = jtf.getText();                            int s = n.lastIndexOf("/");                            String tar = n.substring((s+1));                            URL url = new URL(jtf.getText());                            URLConnection uc = url.openConnection();                            long total = uc.getContentLength();                            InputStream is = uc.getInputStream();                            BufferedInputStream bis = new BufferedInputStream(is,1024*1024*10);                            FileOutputStream fos = new FileOutputStream(tar,true);                            BufferedOutputStream bos = new BufferedOutputStream(fos,1024*1024*10);                            File tarFile = new File(tar);                            long count = tarFile.length();                            is.skip(count);                            byte[] data = new byte[1024*1024*10];                            int (Stri 

热点排行