package com.wgtgt; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; public class DownLoadThread extends Thread{ public DownLoadThread(String path,String tempFolder, int startIndex, int endIndex, int threadId) { try { URL url=new URL(path); HttpURLConnection conn=(HttpURLConnection)url.openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("POST"); conn.setRequestProperty("Range", "bytes="+startIndex+"-"+endIndex); InputStream is=conn.getInputStream(); String fileName=conn.getURL().getFile(); fileName=fileName.substring(fileName.lastIndexOf("/")+1); String fullName=tempFolder+"/"+fileName+threadId; write(fullName, is); } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("线程"+threadId+"下载结束,如果所有线程均下载完,请合并文件"); } private void write(String fullName, InputStream is){ OutputStream os=null; try { os=new FileOutputStream(fullName); byte[]buffer=new byte[1024]; int len=0; while((len=is.read(buffer))!=-1){ os.write(buffer, 0, len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(os!=null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } } }

duxing LV2
2022年6月10日
mylzdy LV12
2022年5月21日
顾北城 LV12
2022年5月8日
zhenghongixin4065 LV9
2021年1月25日
3063686836 LV4
2020年7月16日
liuyilin9608 LV15
2020年5月17日
黄丹丹 LV2
2020年1月17日
wangyan110 LV2
2019年11月7日
初秋若寒 LV10
2019年10月15日
倪卟懂 LV18
2019年7月23日

暂无贡献等级
snowing_for LV9
2024年11月25日
liu1221 LV1
2024年4月20日
Small CN LV7
2023年1月12日
heqian LV17
2022年10月17日
我们都是小怪兽 LV3
2022年9月6日
MoonSight LV1
2022年7月1日
xuexizhuanyong23 LV16
2022年6月29日
lingqianjue LV3
2022年6月14日
duxing LV2
2022年6月10日