DownloadFile.java
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownloadFile {
private File file;
private String url;
private int startPos;
private int endPos;
private int totalSize;
private String fileName;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getStartPos() {
return startPos;
}
public void setStartPos(int startPos) {
this.startPos = startPos;
}
public int getEndPos() {
return endPos;
}
public void setEndPos(int endPos) {
this.endPos = endPos;
}
public int getTotalSize() {
return totalSize;
}
public void setTotalSize(int totalSize) {
this.totalSize = totalSize;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public int getURLTotalSize() throws Exception{
int total = 0;
if(this.url != null && !"".equals(this.url)){
URL url = new URL(this.url);
HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
return httpConn.getContentLength();
}
return total;
}
public InputStream getInputStream(){
InputStream is = null;
try{
if(this.url != null && !"".equals(this.url)){
URL url = new URL(this.url);
HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
httpConn.setRequestProperty("Connection", "Keep-Alive"); //保持一直连接
httpConn.setConnectTimeout(60 * 1000 * 5); //连接超时5分钟
httpConn.setRequestMethod("POST"); //以GET方式连接
httpConn.setAllowUserInteraction(true);
if(this.startPos != 0 || this.endPos != 0){
httpConn.setRequestProperty("Range", "bytes=" + getStartPos() + "-" + getEndPos());
}
return new BufferedInputStream(httpConn.getInputStream(),1024*8);
}
}catch(Exception ex){
ex.printStackTrace();
}
return is;
}
public static void main(String[] args) throws Exception {
DownloadFile downloadFile = new DownloadFile();
downloadFile.setUrl("http://localhost:8080/navigater/admin/123.txt");
downloadFile.setStartPos(5);
downloadFile.setEndPos(880);
InputStream is = downloadFile.getInputStream();
System.out.println("is------>" +is.available());
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String str = null;
while(null != (str = br.readLine())){
System.out.println(str);
}
}
}
Test.java
import java.io.BufferedInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Test {
public static void main(String[] args) throws Exception {
/* InputStream file = new FileInputStream("c:" + File.separator + "123.txt");
byte[] by = new byte[5];
System.out.println(file.markSupported());
// file.mark(1);
// file.reset();
file.skip(5);
int length = file.read(by,0,5);
System.out.println(length);
System.out.println(new String(by));*/
Test test = new Test();
test.saveFile();
}
public void saveFile() throws Exception{
File file = new File("c:" + File.separator + "456.exe");
if(file.exists())file.delete();
// RandomAccessFile randomFile = new RandomAccessFile(file,"rwd");
URL url = new URL("http://localhost:8080/navigater/admin/MyEclipse_6.5.0GA_E3.3.2_Installer_A.exe");
HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
int totalSize = httpConn.getContentLength();
System.out.println("totalSize---------->" + totalSize);
int threadNum = (int)Math.ceil(totalSize/(1024*800f));
System.out.println("threadNum---------->" + threadNum);
CountDownLatch threadCount = new CountDownLatch (threadNum);
ExecutorService service = Executors.newFixedThreadPool(4);
for(int i=0; i<threadNum; i++){
RandomAccessFile randomFile = new RandomAccessFile(file,"rwd");
DownloadFile downloadFile = new DownloadFile();
downloadFile.setUrl("http://localhost:8080/navigater/admin/MyEclipse_6.5.0GA_E3.3.2_Installer_A.exe");
downloadFile.setStartPos(i*1024*800);
int ends = (i+1)*1024*800-1;
if(ends<totalSize){
downloadFile.setEndPos(ends);
}else{
downloadFile.setEndPos(totalSize-1);
}
downloadFile.setTotalSize(totalSize);
randomFile.seek(downloadFile.getStartPos());
service.execute(new MyThread(randomFile,downloadFile, threadCount));
}
threadCount.await();
// randomFile.close();
service.shutdown();
System.out.println("全部完成");
}
class MyThread implements Runnable{
private RandomAccessFile randomFile;
private DownloadFile downloadFile;
private CountDownLatch threadCount;
public MyThread() {
}
public MyThread(RandomAccessFile randomFile, DownloadFile downloadFile, CountDownLatch threadCount) {
this.randomFile = randomFile;
this.downloadFile = downloadFile;
this.threadCount = threadCount;
}
@Override
public void run() {
try{
InputStream is = this.downloadFile.getInputStream();
byte[] by = new byte[1024*40];
int length = 0;
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
while(-1 != (length = is.read(by, 0, by.length))){
// this.randomFile.seek(this.downloadFile.getStartPos());
this.randomFile.write(by,0,length);
// baos.write(by,0,length);
}
// System.out.println(this.downloadFile.getStartPos()+ "~~~~~~~~~~~~~~~~~" + this.downloadFile.getEndPos()+ "~~~~~~~~~~~~~~~~~" + new String(baos.toByteArray()));
is.close();
threadCount.countDown();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}