String directoryName = Environment.getExternalStorageDirectory().toString() + "/filename";////文件保存路径 ///传入参数:Context对象,下载地址, 文件保存路径; DownloadTask downloadTask = new DownloadTask (this, mDownloadUrl, directoryName); new Thread(downloadTask ).start();///////启动线程进行下载
////下载类 public class DownloadTask implements Runnable { private long mDownloadedSize = 0; private long mTotalSize; private int mDownloadPercent; private String mLocalPath; private String mURL; private Context mContext; public DownloadTask (Context context, String url, String localPath) { this.mLocalPath = localPath; this.mURL = url; this.mContext = context; } @Override public void run() { download(); }; ////下载方法 protected boolean download() { File file = new File(mLocalPath); if (file.exists()) { mDownloadedSize = file.length(); } else { mDownloadedSize = 0; } Log.d(TAG, "mURL, " + mURL + " downloadedSize, " + mDownloadedSize); HttpURLConnection httpConnection = null; URL url = null; try { url = new URL(mUpgradeURL); httpConnection = (HttpURLConnection) url.openConnection(); mTotalSize = httpConnection.getContentLength(); Log.d(TAG, "totalSize, " + mTotalSize); if (mDownloadedSize == mTotalSize ) { ////////已下载到本地 return true; } else if (mDownloadedSize > mTotalSize) { if (!file.delete()) { return false; } } httpConnection.disconnect(); } catch (Exception e) { e.printStackTrace(); return false; } finally { try { if (httpConnection != null) { httpConnection.disconnect(); } } catch (Exception e) { } } InputStream inStream = null; RandomAccessFile randomAccessFile = null; try { httpConnection = (HttpURLConnection) url.openConnection(); httpConnection.setRequestProperty("Accept", "image/gif, " + "image/jpeg, " + "image/pjpeg, " + "image/pjpeg, " + "application/x-shockwave-flash, " + "application/xaml+xml, " + "application/vnd.ms-xpsdocument, " + "application/x-ms-xbap, " + "application/x-ms-application, " + "application/vnd.ms-excel, " + "application/vnd.ms-powerpoint, " + "application/msword, " + "*/*"); httpConnection.setRequestProperty("Accept-Language", "zh-CN"); httpConnection.setRequestProperty("Referer", mUpgradeURL); httpConnection.setRequestProperty("Charset", "UTF-8"); httpConnection.setRequestProperty("Range", "bytes=" + mDownloadedSize + "-"); httpConnection.setRequestProperty("Connection", "Keep-Alive"); inStream = httpConnection.getInputStream(); File saveFile = new File(mLocalPath); randomAccessFile = new RandomAccessFile(saveFile, "rwd"); randomAccessFile.seek(mDownloadedSize); int offset = 0; int count = 0; int perUnit = (int) mTotalSize / 1024 / 100; byte[] buffer = new byte[1024]; while ((offset = inStream.read(buffer, 0, 1024)) != -1) { randomAccessFile.write(buffer, 0, offset); count++; if (count == perUnit && mDownloadedSize < mTotalSize) { mDownloadPercent = (int) (mDownloadedSize * 100 / mTotalSize); ////////下载百分百mDownloadPercent count = 0; } mDownloadedSize += offset; } if (mDownloadedSize == mTotalSize ) { /////////下载完成 } Log.d(TAG, "download finished."); return true; } catch (Exception e) { e.printStackTrace(); return false; } finally { try { if (inStream != null) { inStream.close(); } if (httpConnection != null) { httpConnection.disconnect(); } if (randomAccessFile != null) { randomAccessFile.close(); } } catch (Exception e) { } } } }
android中支持多种文件类型的下载类,布布扣,bubuko.com
原文地址:http://blog.csdn.net/only_tan/article/details/38301143