Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2088912
  • 博文数量: 519
  • 博客积分: 10070
  • 博客等级: 上将
  • 技术积分: 3985
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-29 14:05
个人简介

只问耕耘

文章分类

全部博文(519)

文章存档

2016年(1)

2013年(5)

2011年(46)

2010年(220)

2009年(51)

2008年(39)

2007年(141)

2006年(16)

我的朋友

分类: Java

2010-03-26 09:52:36

断点续传 Java版(原)功能:可以下载软件,可以下载未完成的软件
如果软件存在,则改名下载,不进行覆盖,以免勿删文件
代码如下:
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 Download {
    public static void down(String URL, long nPos, String savePathAndFile) {
        try {
            URL url = new URL(URL);
            HttpURLConnection httpConnection = (HttpURLConnection) url
                    .openConnection();
            // 设置User-Agent
            httpConnection.setRequestProperty("User-Agent", "NetFox");
            // 设置断点续传的开始位置
            httpConnection.setRequestProperty("RANGE", "bytes=" + nPos);
            // 获得输入流
            InputStream input = httpConnection.getInputStream();
            RandomAccessFile oSavedFile = new RandomAccessFile(savePathAndFile,
                    "rw");
            // 定位文件指针到nPos位置
            oSavedFile.seek(nPos);
            byte[] b = new byte[1024];
            int nRead;
            // 从输入流中读入字节流,然后写到文件中
            while ((nRead = input.read(b, 0, 1024)) > 0) {
                (oSavedFile).write(b, 0, nRead);
            }
            httpConnection.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static long getRemoteFileSize(String url) {
        long size = 0;
        try {
            HttpURLConnection conn = (HttpURLConnection) (new URL(url))
                    .openConnection();
            size = conn.getContentLength();
            conn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return size;
    }
public static void main(String[] args) {
        String url = "http://www.blogjava.net/breezedancer/archive/2007/07/19/131264.html";
        String savePath = "e:\\";
        String fileName = url.substring(url.lastIndexOf("/"));
        String fileNam=fileName;
        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) (new URL(url)).openConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        File file = new File(savePath + fileName);
        // 获得远程文件大小
        long remoteFileSize = getRemoteFileSize(url);
        System.out.println("远程文件大小="+remoteFileSize);
        int i = 0;
        if (file.exists()) {
            // 先看看是否是完整的,完整,换名字,跳出循环,不完整,继续下载
            long localFileSize = file.length();
            System.out.println("已有文件大小为:"+localFileSize);
            if (localFileSize < remoteFileSize) {
                System.out.println("文件续传");
                down(url, localFileSize, savePath + fileName);
            }else{
                System.out.println("文件存在,重新下载");
                do{
                    i++;
                    fileName = fileNam.substring(0, fileNam.indexOf(".")) + "(" + i
                            + ")" + fileNam.substring(fileNam.indexOf("."));
                   
                    file = new File(savePath + fileName);
                }while(file.exists());
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                down(url, 0, savePath + fileName);
            }
            // 下面表示文件存在,改名字
           
        } else {
            try {
                file.createNewFile();
                System.out.println("下载中");
                down(url, 0, savePath + fileName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }}
阅读(1564) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~