Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android中支持多種文件類型的下載類

android中支持多種文件類型的下載類

編輯:關於Android編程

  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) {
            }
        }
    }
}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved