---
title: "FileDownloadTask"
author: "jiangyang5157"
date: "2011-07-11T13:27:40Z"
tags: []
categories: ["移动开发"]
slug: "filedownloadtask"
draft: false
lastmod: "2011-07-11T13:46:48Z"
id: 1121575
url: "https://jiangyang5157.iteye.com/blog/1121575"
---

```
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.URL;
import java.net.URLConnection;

import android.os.AsyncTask;

public class FileDownloadTask extends AsyncTask<Integer, Integer, Integer> {

 public static final int DOWNLOADING_ERROR = -1;
 public static final int DOWNLOADING_STOP = 0;
 public static final int DOWNLOADING_FINISHED = 1;
 
 private int curStep = DOWNLOADING_STOP;
 
 private URL url = null;
 private File file = null;

 private int startPosition = 0;
 private int endPosition = 0;
 private int curPosition = 0;

 private int downloadSize = 0;

 public FileDownloadTask(URL url, File file, int startPosition,
   int endPosition) {
  this.url = url;
  this.file = file;
  this.startPosition = startPosition;
  this.curPosition = startPosition;
  this.endPosition = endPosition;
 }

 @Override
 protected void onPreExecute() {
  // TODO Auto-generated method stub
 }
 
 @Override
 protected Integer doInBackground(Integer... params) {
  // TODO Auto-generated method stub
  curStep = DOWNLOADING_STOP;
  
  BufferedInputStream bis = null;
  RandomAccessFile raf = null;
  URLConnection connection = null;
  
  byte[] buffer = new byte[params[0]];
  
  try {
   connection = url.openConnection();
   connection.setConnectTimeout(DownloadMessage.CONNECTION_TIMEOUT);
   connection.setAllowUserInteraction(true);
   // 线程下载的起点 & 终点
   connection.setRequestProperty("Range", "bytes=" + startPosition + "-"
     + endPosition);

   raf = new RandomAccessFile(file, "rw");
   raf.seek(startPosition);

   bis = new BufferedInputStream(connection.getInputStream());

   while (curPosition < endPosition) {
    int length = bis.read(buffer, 0, DownloadMessage.DOWNLOADING_BUFFER_SIZE);
    if (length == -1) {
     break;
    }

    raf.write(buffer, 0, length);
    
    publishProgress(length);
   }

   curStep = DOWNLOADING_FINISHED;
  } catch (IllegalArgumentException e) {
   curStep = DOWNLOADING_ERROR;
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   try {
    if (bis != null){
     bis.close();
    }
    if (raf != null){
     raf.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  
  return curStep;
 }
 
 @Override
 protected void onProgressUpdate(Integer... values) {
  // TODO Auto-generated method stub
  super.onProgressUpdate(values);
  
  curPosition = curPosition + values[0];
  
  if (curPosition > endPosition) {
   downloadSize += values[0] - (curPosition - endPosition) + 1;
  } else {
   downloadSize += values[0];
  }
 }

 @Override
 protected void onPostExecute(Integer result) {
  // TODO Auto-generated method stub
  super.onPostExecute(result);
  
  switch(result){
  case DOWNLOADING_ERROR:
   break;
  case DOWNLOADING_STOP:
   break;
  case DOWNLOADING_FINISHED:
   break;
  }
 }

 @Override
 protected void onCancelled() {
  // TODO Auto-generated method stub
  super.onCancelled();
 }
 
 public int getCurStep() {
  return curStep;
 }

 public int getDownloadSize() {
  return downloadSize;
 }
 

 public void setDownloadSize(int downloadSize) {
  this.downloadSize = downloadSize;
 }
 
 public void setPosition(int startPosition, int endPosition){
  this.startPosition = startPosition;
  this.curPosition = startPosition;
  this.endPosition = endPosition;
 }
}
```