码迷,mamicode.com
首页 > 移动开发 > 详细

android中支持多种文件类型的下载类

时间:2017-05-29 21:06:29      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:cto   new t   文件   tar   private   wav   load   buffer   random   

  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中支持多种文件类型的下载类

标签:cto   new t   文件   tar   private   wav   load   buffer   random   

原文地址:http://www.cnblogs.com/jzdwajue/p/6918952.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!