1,使用MD5,本地文件下载完后去文件的md5值与服务器的该文件md5进行比对,一致证明下载完全。
mMusicSourceFile = new File(MUSIC_FILE_PATH, mCurrentPlayMusic.getMusicName()+".tmp"); //按照SourceFile下载 mMusicDescFile = new File(MUSIC_FILE_PATH, mCurrentPlayMusic.getMusicName());
mClient.downloadFile(new HttpGet(mCurrentPlayMusic.getMusicPath()), mMusicSourceFile)
//下载成功后 mMusicSourceFile.renameTo(mMusicDescFile);
int len, mHasRead = 0; byte[] buffer = new byte[8192]; final long size = response.getEntity().getContentLength(); BufferedOutputStream outputStream = null; InputStream inputStream = response.getEntity().getContent(); try { outputStream = new BufferedOutputStream(new FileOutputStream(mFile)); while (!Thread.interrupted() && (len = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); mHasRead += len; } outputStream.flush(); return mHasRead == size; } finally { if (outputStream != null) outputStream.close(); if (inputStream != null) inputStream.close(); }