import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class FileDownload extends Thread{
private static String source;
private static File target;
private long start;
private long end;
static HttpURLConnection con;
static URL url;
public FileDownload(String source, File target, long start, long end) {
super();
this.source = source;
this.target = target;
this.start = start;
this.end = end;
}
@Override
public void run() {
int count = 0;
InputStream input = null;
RandomAccessFile output = null;
long i = 0 ;
try {
int code = con.getResponseCode();
if(code == HttpURLConnection.HTTP_OK){
input = con.getInputStream();
output = new RandomAccessFile(new File(target,"plmm.jpg"), "rw");
// input.skip(start);
output.seek(start);
byte[] b = new byte[128];
int len = 0;
while((len = input.read(b))!=-1&&count<=(end - start)){
count += len ;
output.write(b, 0, len);
i = output.getFilePointer();
}
}
}
catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(input != null)input.close();
if(output != null)output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
source = "http://192.168.46.254:8888/easyBuy/images/plmm.jpg";
target = new File("C:\\Documents and Settings\\Administrator\\桌面");
url = new URL(source);
con = (HttpURLConnection)url.openConnection();
long item = con.getContentLength()/4;
for(int i = 0;i < 4 ; i++){
new FileDownload(source,target,i*item,(i+1)*item).start();
}
}
}