由于需求所至,为了保证文件下载的完整性,需要对文件进行安全校验,故在此简单的实现了下DownLoadManager下载文件并伴有 安全校验的功能
需求简述:
1:下载文件,保证文件下载的完整性需要有MD5校验
2:不能重复下载文件(如果有文件,就不下载)
首先是是DownLoadManager对文件的下载,单个文件的下载问题不大,关键是多个文件同时下载,会出现信息匹配错乱的问题
public class FileManager { private static String sdCard = Environment.getExternalStorageDirectory().toString(); private static FileManager instance; private Context context; private static DownloadManager mDownLoadService; private static String mType; private static LongSparseArray<SourceBean> downloadQueue; private FileManager(Context context) { this.context = context; downloadQueue = new LongSparseArray<>(); } public static FileManager getInatance(Context context) { if (instance == null) { instance = new FileManager(context); } return instance; } /** * 保存文件 * * @param savePath * @param sourceBean */ public void downFile(String savePath, SourceBean sourceBean) { String name = sourceBean.getName(); String md5 = sourceBean.getMd5(); String url = sourceBean.getUrl(); File file = new File(savePath + name); if (file.exists() && md5.equals(getFileMD5(savePath + name))) { Log.i("xd----------", "文件存在,无需下载"); } else if (file.exists() && !md5.equals(getFileMD5(savePath + name))) { file.delete(); Log.i("xd----------", "文件以改变,需下载"); downloadfile(savePath, sourceBean); } else { Log.i("xd----------", "没有文件,需下载"); downloadfile(savePath, sourceBean); } } /** * 从网络端下载文件 * * @param savePath * @param sourceBean */ private void downloadfile(String sav