当前位置: 代码迷 >> J2SE >> 断点续传时先测出已上载文件的大小,再用InputStream的skip()跳转可以吗
  详细解决方案

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

热度:4511   发布时间:2013-02-25 00:00:00.0
断点续传时先测出已下载文件的大小,再用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
  相关解决方案