文件下载的常规操作

1、首先从服务端获取文件信息:比如文件大小、文件名称、文件的MD5值,文件的网络地址等

2、将从服务端获取到的文件信息与本地做对比,首先通过文件名来判断本地文件是否存在(是否下载过文件),如果不存在同名文件可直接根据服务端返回的文件路径进行下载

3、如果本地存在同名文件(下载过文件)比对文件的MD5值,java获取本地文件MD5值的方法:

private static String getFileMD5(String path) {
        String md5 = null;
        try {
            File file = new File(path);
            FileInputStream fis = new FileInputStream(file);
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] buffer = new byte[1024];
            int length = -1;
            while ((length = fis.read(buffer, 0, 1024)) != -1) {
                md.update(buffer, 0, length);
            }
            BigInteger bigInt = new BigInteger(1, md.digest());
            System.out.println("文件md5值:" + bigInt.toString(16));
            md5 = bigInt.toString(16);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return md5;
    }

将获取到的本地文件的MD5值与服务端返回的同名文件做对比,如果MD5值相同(表示是同一个文件)可跳过(不用下载),否则需要开启下载任务

猜你喜欢

转载自blog.csdn.net/coward_/article/details/81409078