那645字节上哪里去了?import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;public class Main {
public static final int MAXSIZE = 1000;
URL url = null;
HttpURLConnection httpURLConnection = null;
RandomAccessFile raf = null; public Main(String url) {
try {
this.url = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
} public void launch() {
String urlName = url.toString();
String fileName = urlName.substring(urlName.lastIndexOf("/"));
fileName = "D:\\" + fileName;
File file = new File(fileName); try {
file.createNewFile();
raf = new RandomAccessFile(file, "rw");
httpURLConnection = (HttpURLConnection) url.openConnection();
int contentSize = httpURLConnection.getContentLength();
System.out.println(contentSize);
boolean flag = true;
int count = 0;
int recent = 0;
count = contentSize / MAXSIZE;
if((recent = contentSize - count * MAXSIZE) != 0){
flag = false;
}
for(int i =0;i < count;i++){
if(i != count - 1){
new MyThread(raf,urlName,i * MAXSIZE,MAXSIZE).start();
} else {
if(flag == false){
new MyThread(raf,urlName,i * MAXSIZE,recent).start();
} else {
new MyThread(raf,urlName,i * MAXSIZE,MAXSIZE).start();
}
}
}

Runtime.getRuntime().addShutdownHook(new Thread() { public void run() {
try {
System.out.println(raf.length());
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
Main main = new Main("http://www.programfan.com/down/2005/masm5.zip");
main.launch();
}
}import java.io.DataInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;public class MyThread extends Thread {
public static int ountSize = 0;
RandomAccessFile raf = null;
URL url = null;
HttpURLConnection httpURLConnection = null;
int i = 0;
int startPos = 0;
int size = 0;
public MyThread(RandomAccessFile raf, String urlName, int i) {
this.raf = raf;
try {
url = new URL(urlName);
this.i = i;
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public MyThread(RandomAccessFile raf, String urlName,int startPos,int size) {
this.raf = raf;
try {
url = new URL(urlName);
this.i = i;
this.startPos = startPos;
this.size = size;
//System.out.println(this.size);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public  void run() { try {
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.disconnect();
byte[] bytes;

bytes = new byte[this.size];
httpURLConnection.setRequestProperty("Range", "bytes="
+ startPos + "-" + (startPos + size));

DataInputStream dis = new DataInputStream(httpURLConnection
.getInputStream());
dis.read(bytes);
httpURLConnection.disconnect();
synchronized (raf) {
raf.seek(startPos);
raf.write(bytes);
}

} catch (IOException e) {
e.printStackTrace();
} }}结果:178645
     178000

解决方案 »

  1.   

    count = contentSize / MAXSIZE应该是
    count = (int)Math.ceil(1.0 * contentSize / MAXSIZE)
      

  2.   

    答:我将你的程序改了几处,程序运行正常.参考代码:(只改了MyThread类)import java.io.DataInputStream;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;public class MyThread extends Thread {
        public static int ountSize = 0;
        RandomAccessFile raf = null;
        URL url = null;
        HttpURLConnection httpURLConnection = null;
        int i = 0;
        int startPos = 0;
        int size = 0;
        public MyThread(RandomAccessFile raf, String urlName, int i) {
            this.raf = raf;
            try {
                url = new URL(urlName);
                this.i = i;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
        public MyThread(RandomAccessFile raf, String urlName,int startPos,int size) {
            this.raf = raf;
            try {
                url = new URL(urlName);
                this.i = i;
                this.startPos = startPos;
                this.size = size;
                //System.out.println(this.size);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
            public  void run() {        try {
                httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.disconnect();
                byte[] bytes;
                
                    bytes = new byte[this.size];
                    httpURLConnection.setRequestProperty("Range", "bytes="
                            + startPos + "-" + (startPos + size));            
                
                httpURLConnection.setConnectTimeout(120000); 
                httpURLConnection.setReadTimeout(120000);
                DataInputStream dis = new DataInputStream(httpURLConnection
                        .getInputStream());
               int len=bytes.length;
               int n=0,pos=0;
               while( (n=dis.read(bytes,pos,len-pos))!=-1  ){
                if(n==0) continue;
                pos = pos + n;
               
                if(len==pos) break;
               }
                httpURLConnection.disconnect();
                synchronized (raf) {
                    raf.seek(startPos);
                    raf.write(bytes);
                }
                
            } catch (IOException e) {
                e.printStackTrace();
            }    }}
    程序运行结果:
    178645
    178645
    下载的文件masm5.zip打开正常.
      

  3.   

    分段读取
     count = contentSize / MAXSIZE;改成
     count = (contentSize+MAXSIZE-1) / MAXSIZE;这样就可以了。因为你的注意读取不足一个分段的部分
      

  4.   

    答:好的.这两个程序我运行好多次了,结果都正确.import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;public class Main {
        public static final int MAXSIZE = 1000;
        URL url = null;
        HttpURLConnection httpURLConnection = null;
        RandomAccessFile raf = null;    public Main(String url) {
            try {
                this.url = new URL(url);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }    public void launch() {
            String urlName = url.toString();
            String fileName = urlName.substring(urlName.lastIndexOf("/"));
            fileName = "D:\\" + fileName;
            File file = new File(fileName);        try {
                file.createNewFile();
                raf = new RandomAccessFile(file, "rw");
                httpURLConnection = (HttpURLConnection) url.openConnection();
                int contentSize = httpURLConnection.getContentLength();
                System.out.println(contentSize);
                boolean flag = true;
                int count = 0;
                int recent = 0;
                count = contentSize / MAXSIZE;
                recent=contentSize - count * MAXSIZE;
                int i=0;
                for( i =0;i < count;i++){
                        new MyThread(raf,urlName,i * MAXSIZE,MAXSIZE).start();                   
                    }
                if(recent>0){
                  new MyThread(raf,urlName,i * MAXSIZE,recent).start();
                }
                Runtime.getRuntime().addShutdownHook(new Thread() {                public void run() {
                        try {
                            System.out.println(raf.length());
                            raf.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }    public static void main(String[] args) {
            Main main = new Main("http://www.programfan.com/down/2005/masm5.zip");
            main.launch();
        }
    }
    import java.io.DataInputStream;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;public class MyThread extends Thread {
        public static int ountSize = 0;
        RandomAccessFile raf = null;
        URL url = null;
        HttpURLConnection httpURLConnection = null;
        int i = 0;
        int startPos = 0;
        int size = 0;
        public MyThread(RandomAccessFile raf, String urlName, int i) {
            this.raf = raf;
            try {
                url = new URL(urlName);
                this.i = i;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
        public MyThread(RandomAccessFile raf, String urlName,int startPos,int size) {
            this.raf = raf;
            try {
                url = new URL(urlName);
                this.i = i;
                this.startPos = startPos;
                this.size = size;
                //System.out.println(this.size);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
            public  void run() {        try {
                httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.disconnect();
                byte[] bytes;
                
                    bytes = new byte[this.size];
                    httpURLConnection.setRequestProperty("Range", "bytes="
                            + startPos + "-" + (startPos + size));            
                
                httpURLConnection.setConnectTimeout(120000); 
                httpURLConnection.setReadTimeout(120000);
                DataInputStream dis = new DataInputStream(httpURLConnection
                        .getInputStream());
               int len=bytes.length;
               int n=0,pos=0;
               while( (n=dis.read(bytes,pos,len-pos))!=-1  ){
                if(n==0  ) continue;
                pos = pos + n;
               
                if(len==pos) break;
               }
                httpURLConnection.disconnect();
                synchronized (raf) {
                    raf.seek(startPos);
                    raf.write(bytes);
                }
                
            } catch (IOException e) {
                e.printStackTrace();
            }    }}