1 import java.io.ByteArrayOutputStream; 2 import java.io.InputStream; 3 4 public class StreamTool { 5 /** 6 * 把一个inputstream里面的内容转化成一个byte[] 7 */ 8 public static byte[] getBytes(InputStream is) throws Exception { 9 ByteArrayOutputStream bos = new ByteArrayOutputStream();10 byte[] buffer = new byte[1024];11 int len = 0;12 while ((len = is.read(buffer)) != -1) {13 bos.write(buffer, 0, len);14 }15 is.close();16 bos.flush();17 byte[] result = bos.toByteArray();18 System.out.println(new String(result));19 return result;20 }21 }
1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.FileOutputStream; 4 import java.io.InputStream; 5 import java.io.RandomAccessFile; 6 import java.net.HttpURLConnection; 7 import java.net.URL; 8 9 public class TestDownload { 10 public static final String path = "http://192.168.1.100:8080/aaa.exe"; 11 12 public static void main(String[] args) throws Exception { 13 URL url = new URL(path); 14 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 15 conn.setRequestMethod("GET"); 16 conn.setConnectTimeout(5000); 17 conn.setRequestProperty("User-Agent", 18 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); 19 int code = conn.getResponseCode(); 20 if (code == 200) { 21 int len = conn.getContentLength(); 22 RandomAccessFile file = new RandomAccessFile( 23 "C:/Users/DELL/Desktop/" + getFilenName(path), "rwd"); 24 // 1.设置本地文件大小跟服务器的文件大小一致 25 file.setLength(len); 26 27 // 2 .假设开启3 个线程 28 int threadnumber = 3; 29 int blocksize = len / threadnumber; 30 /** 31 * 线程1 0~ blocksize 线程2 1*bolocksize ~ 2*blocksize 线程3 2 *blocksize ~ 32 * 文件末尾 33 */ 34 for (int i = 0; i < threadnumber; i++) { 35 int startposition = i * blocksize; 36 int endpositon = (i + 1) * blocksize; 37 if (i == (threadnumber - 1)) { 38 // 最后一个线程 39 endpositon = len; 40 } 41 DownLoadTask task = new DownLoadTask( i, path, startposition,endpositon); 43 task.start(); 44 } 45 } 46 } 47 48 public static String getFilenName(String path) { 49 int start = path.lastIndexOf("/") + 1; 50 return path.substring(start, path.length()); 51 } 52 53 } 54 55 class DownLoadTask extends Thread { 56 public static final String path = "http://192.168.1.100:8080/aaa.exe"; 57 int threadid; 58 String filepath; 59 int startposition; 60 int endpositon; 61 62 public DownLoadTask(int threadid, String filepath, int startposition, int endpositon) { 64 this.threadid = threadid; 65 this.filepath = filepath; 66 this.startposition = startposition; 67 this.endpositon = endpositon; 69 } 70 71 @Override 72 public void run() { 73 try { 74 File postionfile = new File(threadid + ".txt"); 75 URL url = new URL(filepath); 76 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 77 System.out.println("线程" + threadid + "正在下载 " + "开始位置 : " 78 + startposition + "结束位置 " + endpositon); 79 80 // 断点操作 81 if (postionfile.exists()) { 82 FileInputStream fis = new FileInputStream(postionfile); 83 byte[] result = StreamTool.getBytes(fis); 84 int newstartposition = Integer.parseInt(new String(result)); 85 if (newstartposition > startposition) { // 如果新的位置 > 开始位置。 86 startposition = newstartposition; 87 } 88 } 89 90 // "Range", "bytes=2097152-4194303") 91 conn.setRequestProperty("Range", "bytes=" + startposition + "-" 92 + endpositon); 93 conn.setRequestMethod("GET"); 94 conn.setConnectTimeout(5000); 95 conn.setRequestProperty("User-Agent", 96 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); 97 InputStream is = conn.getInputStream(); 98 RandomAccessFile file = new RandomAccessFile( 99 "C:/Users/DELL/Desktop/" + getFilenName(path), "rwd");100 // 设置 数据从文件哪个位置开始写101 file.seek(startposition);102 byte[] buffer = new byte[1024];103 int len = 0;104 105 // 代表当前读到的服务器数据的位置 ,同时这个值已经存储的文件的位置106 int currentPostion = startposition;107 108 // 创建一个文件对象 ,记录当前某个文件的下载位置109 while ((len = is.read(buffer)) != -1) {110 file.write(buffer, 0, len);111 112 currentPostion += len;113 // 需要把currentPostion 信息给持久化到存储设备114 String position = currentPostion + "";115 System.out.println("线程 .." + threadid + "....开始位置..."116 + startposition + "..当前位置..." + currentPostion117 + "...结束位置...." + endpositon);118 FileOutputStream fos = new FileOutputStream(postionfile);119 fos.write(position.getBytes());120 fos.flush();121 fos.close();122 }123 file.close();124 System.out.println("线程" + threadid + "下载完毕");125 // 当线程下载完毕后 把文件删除掉126 if (postionfile.exists()) {127 postionfile.delete();128 }129 } catch (Exception e) {130 e.printStackTrace();131 }132 super.run();133 }134 135 public static String getFilenName(String path) {136 int start = path.lastIndexOf("/") + 1;137 return path.substring(start, path.length());138 }139 140 }
1 public class test1 {2 public static void main(String[] args) throws Exception {3 RandomAccessFile file = new RandomAccessFile("haha.txt", "rwd");4 file.setLength(1024 * 1024);5 }6 }