package threadFileCopy;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class Test {
public static void main(String[] args) throws IOException {
String s = "http://img5.duitang.com/uploads/item/201512/15/20151215223141_icL2m.jpeg";
URL url = new URL(s);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(500);
long len = conn.getContentLength();
System.out.println("响应数据的字节数:" + len);
int code = conn.getResponseCode();
if (code == HttpURLConnection.HTTP_OK) {
File dir = new File("C:\\Users\\rhs\\Desktop", s.substring(s.lastIndexOf("/")));
long l = len / 4;
int count = 4;
for (int i = 0; i < count; i++) {
FileCopy fc = null;
if (i == count - 1) {
fc = new FileCopy(url, dir, i * l, len);
} else {
fc = new FileCopy(url, dir, i * l, (i + 1) * l - 1);
}
fc.start();
}
Schedule sd = new Schedule(len, dir);
sd.setDaemon(true);
sd.start();
}
}
}
package threadFileCopy;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class FileCopy extends Thread {
private URL url;
private File dir;
private long start;
private long end;
private File file = new File(this.getName() + ".txt");
public FileCopy(URL url, File dir, long start, long end) {
super();
this.url = url;
this.dir = dir;
this.start = start;
this.end = end;
}
@Override
public void run() {
RandomAccessFile write = null;
RandomAccessFile write1 = null;
RandomAccessFile read = null;
InputStream is = null;
try {
if (!file.exists()) {
write1 = new RandomAccessFile(file, "rw");
write1.writeLong(start);
}
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(500);
write = new RandomAccessFile(dir, "rw");
is = conn.getInputStream();
write1 = new RandomAccessFile(file, "rw");
read = new RandomAccessFile(file, "r");
start = read.readLong();
System.out.println(start);
is.skip(start);
write.seek(start);
;
byte[] b = new byte[1024];
int len = 0;
while (((len = is.read(b)) != -1) && start < end) {
write.write(b, 0, len);
start += len;
write1.seek(0);
write1.writeLong(start);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
write.close();
is.close();
sleep(100);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package threadFileCopy;
import java.io.File;
import java.text.DecimalFormat;
public class Schedule extends Thread {
private long len;
private File dir;
public Schedule(long len, File dir) {
super();
this.len = len;
this.dir = dir;
}
@Override
public void run() {
DecimalFormat df = new DecimalFormat("##.##%");
long length = len;
long nowlength = 0;
while (nowlength < length) {
nowlength = dir.length();
System.out.println(df.format(nowlength * 1.0 / length));
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}