码迷,mamicode.com
首页 > 其他好文 > 详细

断点续传和下载原理分析

时间:2016-06-23 15:49:58      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

断点续传和断点下载都是用的RandomAccessFile, 它具有移动指定的文件大小的位置的功能seek 。

断点续传是由服务器给客户端一个已经上传的位置标记position,然后客户端再将文件指针移动到相应的position,通过输入流将文件剩余部分读出来传输给服务器

断点下载 是由客户端告诉服务器已经下载的大小,然后服务器会将指针移动到相应的position,继续读出,把文件返回给客户端。 当然为了下载的更快一下,也可以多线程下载,那么基本实现就是给每个线程分配固定的字节的文件,分别去读

 

首先是文件上传,这个要用到服务器


技术分享

关键代码:


 FileServer.java

 

Java代码  技术分享
  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.OutputStream;  
  7. import java.io.PushbackInputStream;  
  8. import java.io.RandomAccessFile;  
  9. import java.net.ServerSocket;  
  10. import java.net.Socket;  
  11. import java.text.SimpleDateFormat;  
  12. import java.util.Date;  
  13. import java.util.HashMap;  
  14. import java.util.Map;  
  15. import java.util.Properties;  
  16. import java.util.Set;  
  17. import java.util.concurrent.ExecutorService;  
  18. import java.util.concurrent.Executors;  
  19.   
  20. import util.FileLogInfo;  
  21. import util.StreamTool;  
  22.   
  23.   
  24.   
  25. public class FileServer {  
  26.      private ExecutorService executorService;//线程池  
  27.      private int port;//监听端口  
  28.      private boolean quit = false;//退出  
  29.      private ServerSocket server;  
  30.      private Map<Long, FileLogInfo> datas = new HashMap<Long, FileLogInfo>();//存放断点数据,以后改为数据库存放  
  31.      public FileServer(int port)  
  32.      {  
  33.          this.port = port;  
  34.          //创建线程池,池中具有(cpu个数*50)条线程  
  35.          executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 50);  
  36.      }  
  37.        
  38.     /** 
  39.       * 退出 
  40.       */  
  41.      public void quit()  
  42.      {  
  43.         this.quit = true;  
  44.         try   
  45.         {  
  46.             server.close();  
  47.         }catch (IOException e)   
  48.         {  
  49.             e.printStackTrace();  
  50.         }  
  51.      }  
  52.        
  53.      /** 
  54.       * 启动服务 
  55.       * @throws Exception 
  56.       */  
  57.      public void start() throws Exception  
  58.      {  
  59.          server = new ServerSocket(port);//实现端口监听  
  60.          while(!quit)  
  61.          {  
  62.              try   
  63.              {  
  64.                Socket socket = server.accept();  
  65.                executorService.execute(new SocketTask(socket));//为支持多用户并发访问,采用线程池管理每一个用户的连接请求  
  66.              }catch (Exception e)   
  67.              {  
  68.                  e.printStackTrace();  
  69.              }  
  70.          }  
  71.      }  
  72.        
  73.      private final class SocketTask implements Runnable  
  74.      {  
  75.         private Socket socket = null;  
  76.         public SocketTask(Socket socket)   
  77.         {  
  78.             this.socket = socket;  
  79.         }  
  80.         @Override  
  81.         public void run()   
  82.         {  
  83.             try   
  84.             {  
  85.                 System.out.println("FileServer accepted connection "+ socket.getInetAddress()+ ":"+ socket.getPort());  
  86.                 //得到客户端发来的第一行协议数据:Content-Length=143253434;filename=xxx.3gp;sourceid=  
  87.                 //如果用户初次上传文件,sourceid的值为空。  
  88.                 InputStream inStream = socket.getInputStream();  
  89.                 String head = StreamTool.readLine(inStream);  
  90.                 System.out.println("FileServer head:"+head);  
  91.                 if(head!=null)  
  92.                 {  
  93.                     //下面从协议数据中提取各项参数值  
  94.                     String[] items = head.split(";");  
  95.                     String filelength = items[0].substring(items[0].indexOf("=")+1);  
  96.                     String filename = items[1].substring(items[1].indexOf("=")+1);  
  97.                     String sourceid = items[2].substring(items[2].indexOf("=")+1);        
  98.                     //生成资源id,如果需要唯一性,可以采用UUID  
  99.                     long id = System.currentTimeMillis();  
  100.                     FileLogInfo log = null;  
  101.                     if(sourceid!=null && !"".equals(sourceid))  
  102.                     {  
  103.                         id = Long.valueOf(sourceid);  
  104.                         //查找上传的文件是否存在上传记录  
  105.                         log = find(id);  
  106.                     }  
  107.                     File file = null;  
  108.                     int position = 0;  
  109.                     //如果上传的文件不存在上传记录,为文件添加跟踪记录  
  110.                     if(log==null)  
  111.                     {  
  112.                         //设置存放的位置与当前应用的位置有关  
  113.                         File dir = new File("c:/temp/");  
  114.                         if(!dir.exists()) dir.mkdirs();  
  115.                         file = new File(dir, filename);  
  116.                         //如果上传的文件发生重名,然后进行改名  
  117.                         if(file.exists())  
  118.                         {  
  119.                             filename = filename.substring(0, filename.indexOf(".")-1)+ dir.listFiles().length+ filename.substring(filename.indexOf("."));  
  120.                             file = new File(dir, filename);  
  121.                         }  
  122.                         save(id, file);  
  123.                     }  
  124.                     // 如果上传的文件存在上传记录,读取上次的断点位置  
  125.                     else  
  126.                     {  
  127.                         System.out.println("FileServer have exits log not null");  
  128.                         //从上传记录中得到文件的路径  
  129.                         file = new File(log.getPath());  
  130.                         if(file.exists())  
  131.                         {  
  132.                             File logFile = new File(file.getParentFile(), file.getName()+".log");  
  133.                             if(logFile.exists())  
  134.                             {  
  135.                                 Properties properties = new Properties();  
  136.                                 properties.load(new FileInputStream(logFile));  
  137.                                 //读取断点位置  
  138.                                 position = Integer.valueOf(properties.getProperty("length"));  
  139.                             }  
  140.                         }  
  141.                     }  
  142.                     //***************************上面是对协议头的处理,下面正式接收数据***************************************  
  143.                     //向客户端请求传输数据  
  144.                     OutputStream outStream = socket.getOutputStream();  
  145.                     String response = "sourceid="+ id+ ";position="+ position+ "%";  
  146.                     //服务器收到客户端的请求信息后,给客户端返回响应信息:sourceid=1274773833264;position=position  
  147.                     //sourceid由服务生成,唯一标识上传的文件,position指示客户端从文件的什么位置开始上传  
  148.                     outStream.write(response.getBytes());  
  149.                     RandomAccessFile fileOutStream = new RandomAccessFile(file, "rwd");  
  150.                     //设置文件长度  
  151.                     if(position==0) fileOutStream.setLength(Integer.valueOf(filelength));  
  152.                     //移动文件指定的位置开始写入数据  
  153.                     fileOutStream.seek(position);  
  154.                     byte[] buffer = new byte[1024];  
  155.                     int len = -1;  
  156.                     int length = position;  
  157.                     //从输入流中读取数据写入到文件中,并将已经传入的文件长度写入配置文件,实时记录文件的最后保存位置  
  158.                     while( (len=inStream.read(buffer)) != -1)  
  159.                     {  
  160.                         fileOutStream.write(buffer, 0, len);  
  161.                         length += len;  
  162.                         Properties properties = new Properties();  
  163.                         properties.put("length", String.valueOf(length));  
  164.                         FileOutputStream logFile = new FileOutputStream(new File(file.getParentFile(), file.getName()+".log"));  
  165.                         //实时记录文件的最后保存位置  
  166.                         properties.store(logFile, null);  
  167.                         logFile.close();  
  168.                     }  
  169.                     //如果长传长度等于实际长度则表示长传成功  
  170.                     if(length==fileOutStream.length()){  
  171.                         delete(id);  
  172.                     }  
  173.                     fileOutStream.close();                    
  174.                     inStream.close();  
  175.                     outStream.close();  
  176.                     file = null;  
  177.                 }  
  178.             }  
  179.             catch (Exception e)   
  180.             {  
  181.                 e.printStackTrace();  
  182.             }  
  183.             finally{  
  184.                 try  
  185.                 {  
  186.                     if(socket!=null && !socket.isClosed()) socket.close();  
  187.                 }   
  188.                 catch (IOException e)  
  189.                 {  
  190.                     e.printStackTrace();  
  191.                 }  
  192.             }  
  193.         }  
  194.      }  
  195.        
  196.      /**  
  197.       * 查找在记录中是否有sourceid的文件  
  198.       * @param sourceid  
  199.       * @return  
  200.       */  
  201.      public FileLogInfo find(Long sourceid)  
  202.      {  
  203.          return datas.get(sourceid);  
  204.      }  
  205.        
  206.      /** 
  207.       * 保存上传记录,日后可以改成通过数据库存放 
  208.       * @param id 
  209.       * @param saveFile 
  210.       */  
  211.      public void save(Long id, File saveFile)  
  212.      {  
  213.          System.out.println("save logfile "+id);  
  214.          datas.put(id, new FileLogInfo(id, saveFile.getAbsolutePath()));  
  215.      }  
  216.        
  217.      /** 
  218.       * 当文件上传完毕,删除记录 
  219.       * @param sourceid 
  220.       */  
  221.      public void delete(long sourceid)  
  222.      {  
  223.          System.out.println("delete logfile "+sourceid);  
  224.          if(datas.containsKey(sourceid)) datas.remove(sourceid);  
  225.      }  
  226.        
  227. }  

 由于在上面的流程图中已经进行了详细的分析,我在这儿就不讲了,只是在存储数据的时候服务器没有用数据库去存储,这儿只是为了方便,所以要想测试断点上传,服务器是不能停的,否则数据就没有了,在以后改进的时候应该用数据库去存储数据。

文件上传客户端:


技术分享

关键代码:

UploadActivity.java

 

Java代码  技术分享
  1. package com.hao;  
  2.   
  3. import java.io.File;  
  4. import java.util.List;  
  5.   
  6. import com.hao.upload.UploadThread;  
  7. import com.hao.upload.UploadThread.UploadProgressListener;  
  8. import com.hao.util.ConstantValues;  
  9. import com.hao.util.FileBrowserActivity;  
  10.   
  11. import android.app.Activity;  
  12. import android.app.Dialog;  
  13. import android.app.ProgressDialog;  
  14. import android.content.DialogInterface;  
  15. import android.content.Intent;  
  16. import android.content.res.Resources;  
  17. import android.net.Uri;  
  18. import android.os.Bundle;  
  19. import android.os.Environment;  
  20. import android.os.Handler;  
  21. import android.os.Message;  
  22. import android.util.Log;  
  23. import android.view.View;  
  24. import android.view.View.OnClickListener;  
  25. import android.widget.Button;  
  26. import android.widget.TextView;  
  27. import android.widget.Toast;  
  28. /** 
  29.  *  
  30.  * @author Administrator 
  31.  * 
  32.  */  
  33. public class UploadActivity extends Activity implements OnClickListener{  
  34.     private static final String TAG = "SiteFileFetchActivity";  
  35.     private Button download, upload, select_file;  
  36.     private TextView info;  
  37.     private static final int PROGRESS_DIALOG = 0;  
  38.     private ProgressDialog progressDialog;  
  39.     private UploadThread uploadThread;  
  40.     private String uploadFilePath = null;  
  41.     private String fileName;  
  42.     /** Called when the activity is first created. */  
  43.     @Override  
  44.     public void onCreate(Bundle savedInstanceState) {  
  45.         super.onCreate(savedInstanceState);  
  46.         setContentView(R.layout.upload);  
  47.         initView();  
  48.     }  
  49.       
  50.     private void initView(){  
  51.         download = (Button) findViewById(R.id.download);  
  52.         download.setOnClickListener(this);  
  53.         upload = (Button) findViewById(R.id.upload);  
  54.         upload.setOnClickListener(this);  
  55.         info = (TextView) findViewById(R.id.info);  
  56.         select_file = (Button) findViewById(R.id.select_file);  
  57.         select_file.setOnClickListener(this);  
  58.     }  
  59.       
  60.     @Override  
  61.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  62.         // TODO Auto-generated method stub  
  63.         super.onActivityResult(requestCode, resultCode, data);  
  64.         if (resultCode == RESULT_OK) {  
  65.                   if (requestCode == 1) {  
  66.                            Uri uri = data.getData();    // 接收用户所选文件的路径  
  67.                            info.setText("select: " + uri); // 在界面上显示路径  
  68.                            uploadFilePath = uri.getPath();  
  69.                            int last = uploadFilePath.lastIndexOf("/");  
  70.                            uploadFilePath = uri.getPath().substring(0, last+1);  
  71.                            fileName = uri.getLastPathSegment();  
  72.                   }  
  73.         }  
  74.     }  
  75.       
  76.     protected Dialog onCreateDialog(int id) {  
  77.         switch(id) {  
  78.         case PROGRESS_DIALOG:  
  79.             progressDialog = new ProgressDialog(UploadActivity.this);  
  80.             progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  81.             progressDialog.setButton("暂停", new DialogInterface.OnClickListener() {  
  82.                 @Override  
  83.                 public void onClick(DialogInterface dialog, int which) {  
  84.                     // TODO Auto-generated method stub  
  85.                     uploadThread.closeLink();  
  86.                     dialog.dismiss();  
  87.                 }  
  88.             });  
  89.             progressDialog.setMessage("正在上传...");  
  90.             progressDialog.setMax(100);  
  91.             return progressDialog;  
  92.         default:  
  93.             return null;  
  94.         }  
  95.     }  
  96.       
  97.     /**  
  98.      * 使用Handler给创建他的线程发送消息,  
  99.      * 匿名内部类  
  100.      */    
  101.     private Handler handler = new Handler()    
  102.     {    
  103.         @Override  
  104.         public void handleMessage(Message msg)     
  105.         {    
  106.             //获得上传长度的进度    
  107.             int length = msg.getData().getInt("size");    
  108.             progressDialog.setProgress(length);    
  109.             if(progressDialog.getProgress()==progressDialog.getMax())//上传成功    
  110.             {    
  111.                 progressDialog.dismiss();  
  112.                 Toast.makeText(UploadActivity.this, getResources().getString(R.string.upload_over), 1).show();    
  113.             }    
  114.         }    
  115.     };     
  116.   
  117.     @Override  
  118.     public void onClick(View v) {  
  119.         // TODO Auto-generated method stub  
  120.         Resources r = getResources();  
  121.         switch(v.getId()){  
  122.             case R.id.select_file:  
  123.                 Intent intent = new Intent();  
  124.                 //设置起始目录和查找的类型  
  125.                 intent.setDataAndType(Uri.fromFile(new File("/sdcard")), "*/*");//"*/*"表示所有类型,设置起始文件夹和文件类型  
  126.                 intent.setClass(UploadActivity.this, FileBrowserActivity.class);  
  127.                 startActivityForResult(intent, 1);  
  128.                 break;  
  129.             case R.id.download:  
  130.                 startActivity(new Intent(UploadActivity.this, SmartDownloadActivity.class));  
  131.                 break;  
  132.             case R.id.upload:  
  133.                 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))//判断SDCard是否存在    
  134.                 {    
  135.                     if(uploadFilePath == null){  
  136.                         Toast.makeText(UploadActivity.this, "还没设置上传文件", 1).show();  
  137.                     }  
  138.                     System.out.println("uploadFilePath:"+uploadFilePath+" "+fileName);  
  139.                     //取得SDCard的目录    
  140.                     File uploadFile = new File(new File(uploadFilePath), fileName);    
  141.                     Log.i(TAG, "filePath:"+uploadFile.toString());  
  142.                     if(uploadFile.exists())    
  143.                     {    
  144.                         showDialog(PROGRESS_DIALOG);  
  145.                         info.setText(uploadFile+" "+ConstantValues.HOST+":"+ConstantValues.PORT);  
  146.                         progressDialog.setMax((int) uploadFile.length());//设置长传文件的最大刻度  
  147.                         uploadThread = new UploadThread(UploadActivity.this, uploadFile, ConstantValues.HOST, ConstantValues.PORT);  
  148.                         uploadThread.setListener(new UploadProgressListener() {  
  149.                               
  150.                             @Override  
  151.                             public void onUploadSize(int size) {  
  152.                                 // TODO Auto-generated method stub  
  153.                                 Message msg = new Message();  
  154.                                 msg.getData().putInt("size", size);  
  155.                                 handler.sendMessage(msg);  
  156.                             }  
  157.                         });  
  158.                         uploadThread.start();  
  159.                     }    
  160.                     else    
  161.                     {    
  162.                         Toast.makeText(UploadActivity.this, "文件不存在", 1).show();    
  163.                     }    
  164.                 }    
  165.                 else    
  166.                 {    
  167.                     Toast.makeText(UploadActivity.this, "SDCard不存在!", 1).show();    
  168.                 }    
  169.                 break;  
  170.         }  
  171.               
  172.     }  
  173.       
  174.       
  175. }  

 UploadThread.java

 

Java代码  技术分享
  1. package com.hao.upload;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.OutputStream;  
  7. import java.io.RandomAccessFile;  
  8. import java.net.Socket;  
  9.   
  10. import android.content.Context;  
  11. import android.util.Log;  
  12.   
  13. import com.hao.db.UploadLogService;  
  14. import com.hao.util.StreamTool;  
  15.   
  16. public class UploadThread extends Thread {  
  17.   
  18.     private static final String TAG = "UploadThread";  
  19.     /*需要上传文件的路径*/  
  20.     private File uploadFile;  
  21.     /*上传文件服务器的IP地址*/  
  22.     private String dstName;  
  23.     /*上传服务器端口号*/  
  24.     private int dstPort;  
  25.     /*上传socket链接*/  
  26.     private Socket socket;  
  27.     /*存储上传的数据库*/  
  28.     private UploadLogService logService;   
  29.     private UploadProgressListener listener;  
  30.     public UploadThread(Context context, File uploadFile, final String dstName,final int dstPort){  
  31.         this.uploadFile = uploadFile;  
  32.         this.dstName = dstName;  
  33.         this.dstPort = dstPort;  
  34.         logService = new UploadLogService(context);  
  35.     }  
  36.       
  37.     public void setListener(UploadProgressListener listener) {  
  38.         this.listener = listener;  
  39.     }  
  40.   
  41.     /** 
  42.      * 模拟断开连接 
  43.      */  
  44.     public void closeLink(){  
  45.         try{  
  46.             if(socket != null) socket.close();  
  47.         }catch(IOException e){  
  48.             e.printStackTrace();  
  49.             Log.e(TAG, "close socket fail");  
  50.         }  
  51.     }  
  52.   
  53.     @Override  
  54.     public void run() {  
  55.         // TODO Auto-generated method stub  
  56.         try {  
  57.             // 判断文件是否已有上传记录  
  58.             String souceid = logService.getBindId(uploadFile);  
  59.             // 构造拼接协议  
  60.             String head = "Content-Length=" + uploadFile.length()  
  61.                     + ";filename=" + uploadFile.getName() + ";sourceid="  
  62.                     + (souceid == null ? "" : souceid) + "%";  
  63.             // 通过Socket取得输出流  
  64.             socket = new Socket(dstName, dstPort);  
  65.             OutputStream outStream = socket.getOutputStream();  
  66.             outStream.write(head.getBytes());  
  67.             Log.i(TAG, "write to outStream");  
  68.   
  69.             InputStream inStream = socket.getInputStream();  
  70.             // 获取到字符流的id与位置  
  71.             String response = StreamTool.readLine(inStream);  
  72.             Log.i(TAG, "response:" + response);  
  73.             String[] items = response.split(";");  
  74.             String responseid = items[0].substring(items[0].indexOf("=") + 1);  
  75.             String position = items[1].substring(items[1].indexOf("=") + 1);  
  76.             // 代表原来没有上传过此文件,往数据库添加一条绑定记录  
  77.             if (souceid == null) {  
  78.                 logService.save(responseid, uploadFile);  
  79.             }  
  80.             RandomAccessFile fileOutStream = new RandomAccessFile(uploadFile, "r");  
  81.             // 查找上次传送的最终位置,并从这开始传送  
  82.             fileOutStream.seek(Integer.valueOf(position));  
  83.             byte[] buffer = new byte[1024];  
  84.             int len = -1;  
  85.             // 初始化上传的数据长度  
  86.             int length = Integer.valueOf(position);  
  87.             while ((len = fileOutStream.read(buffer)) != -1) {  
  88.                 outStream.write(buffer, 0, len);  
  89.                 // 设置长传数据长度  
  90.                 length += len;  
  91.                 listener.onUploadSize(length);  
  92.             }  
  93.             fileOutStream.close();  
  94.             outStream.close();  
  95.             inStream.close();  
  96.             socket.close();  
  97.             // 判断上传完则删除数据  
  98.             if (length == uploadFile.length())  
  99.                 logService.delete(uploadFile);  
  100.         } catch (Exception e) {  
  101.             e.printStackTrace();  
  102.         }  
  103.     }  
  104.       
  105.     public interface UploadProgressListener{  
  106.         void onUploadSize(int size);  
  107.     }  
  108. }  

 
 下面是多线程下载


技术分享

SmartDownloadActivity.java

 

Java代码  技术分享
  1. package com.hao;  
  2.   
  3. import java.io.File;  
  4.   
  5. import com.hao.R;  
  6. import com.hao.R.id;  
  7. import com.hao.R.layout;  
  8. import com.hao.download.SmartFileDownloader;  
  9. import com.hao.download.SmartFileDownloader.SmartDownloadProgressListener;  
  10. import com.hao.util.ConstantValues;  
  11.   
  12. import android.app.Activity;  
  13. import android.os.Bundle;  
  14. import android.os.Environment;  
  15. import android.os.Handler;  
  16. import android.os.Message;  
  17. import android.view.View;  
  18. import android.widget.Button;  
  19. import android.widget.ProgressBar;  
  20. import android.widget.TextView;  
  21. import android.widget.Toast;  
  22.   
  23. /** 
  24.  *  
  25.  * @author Administrator 
  26.  *  
  27.  */  
  28. public class SmartDownloadActivity extends Activity {  
  29.     private ProgressBar downloadbar;  
  30.     private TextView resultView;  
  31.     private String path = ConstantValues.DOWNLOAD_URL;  
  32.     SmartFileDownloader loader;  
  33.     private Handler handler = new Handler() {  
  34.         @Override  
  35.         // 信息  
  36.         public void handleMessage(Message msg) {  
  37.             switch (msg.what) {  
  38.             case 1:  
  39.                 int size = msg.getData().getInt("size");  
  40.                 downloadbar.setProgress(size);  
  41.                 float result = (float) downloadbar.getProgress() / (float) downloadbar.getMax();  
  42.                 int p = (int) (result * 100);  
  43.                 resultView.setText(p + "%");  
  44.                 if (downloadbar.getProgress() == downloadbar.getMax())  
  45.                     Toast.makeText(SmartDownloadActivity.this, "下载成功", 1).show();  
  46.                 break;  
  47.             case -1:  
  48.                 Toast.makeText(SmartDownloadActivity.this, msg.getData().getString("error"), 1).show();  
  49.                 break;  
  50.             }  
  51.   
  52.         }  
  53.     };  
  54.   
  55.     public void onCreate(Bundle savedInstanceState) {  
  56.         super.onCreate(savedInstanceState);  
  57.         setContentView(R.layout.download);  
  58.   
  59.         Button button = (Button) this.findViewById(R.id.button);  
  60.         Button closeConn = (Button) findViewById(R.id.closeConn);  
  61.         closeConn.setOnClickListener(new View.OnClickListener() {  
  62.               
  63.             @Override  
  64.             public void onClick(View v) {  
  65.                 // TODO Auto-generated method stub  
  66.                 if(loader != null){  
  67.                     finish();  
  68.                 }else{  
  69.                     Toast.makeText(SmartDownloadActivity.this, "还没有开始下载,不能暂停", 1).show();  
  70.                 }  
  71.             }  
  72.         });  
  73.         downloadbar = (ProgressBar) this.findViewById(R.id.downloadbar);  
  74.         resultView = (TextView) this.findViewById(R.id.result);  
  75.         resultView.setText(path);  
  76.         button.setOnClickListener(new View.OnClickListener() {  
  77.             @Override  
  78.             public void onClick(View v) {  
  79.                 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {  
  80.                     download(path, ConstantValues.FILE_PATH);  
  81.                 } else {  
  82.                     Toast.makeText(SmartDownloadActivity.this, "没有SDCard", 1).show();  
  83.                 }  
  84.             }  
  85.         });  
  86.     }  
  87.   
  88.     // 对于UI控件的更新只能由主线程(UI线程)负责,如果在非UI线程更新UI控件,更新的结果不会反映在屏幕上,某些控件还会出错  
  89.     private void download(final String path, final File dir) {  
  90.         new Thread(new Runnable() {  
  91.             @Override  
  92.             public void run() {  
  93.                 try {  
  94.                     loader = new SmartFileDownloader(SmartDownloadActivity.this, path, dir, 3);  
  95.                     int length = loader.getFileSize();// 获取文件的长度  
  96.                     downloadbar.setMax(length);  
  97.                     loader.download(new SmartDownloadProgressListener() {  
  98.                         @Override  
  99.                         public void onDownloadSize(int size) {// 可以实时得到文件下载的长度  
  100.                             Message msg = new Message();  
  101.                             msg.what = 1;  
  102.                             msg.getData().putInt("size", size);  
  103.                             handler.sendMessage(msg);  
  104.                         }  
  105.                     });  
  106.                 } catch (Exception e) {  
  107.                     Message msg = new Message();// 信息提示  
  108.                     msg.what = -1;  
  109.                     msg.getData().putString("error", "下载失败");// 如果下载错误,显示提示失败!  
  110.                     handler.sendMessage(msg);  
  111.                 }  
  112.             }  
  113.         }).start();// 开始  
  114.   
  115.     }  
  116. }  

这个单个的下载线程

 SmartDownloadThread.java

 

Java代码  技术分享
  1. package com.hao.download;  
  2.   
  3. import java.io.File;  
  4. import java.io.InputStream;  
  5. import java.io.RandomAccessFile;  
  6. import java.net.HttpURLConnection;  
  7. import java.net.URL;  
  8.   
  9. import android.util.Log;  
  10.   
  11. /** 
  12.  * 线程下载 
  13.  * @author Administrator 
  14.  *  
  15.  */  
  16. public class SmartDownloadThread extends Thread {  
  17.     private static final String TAG = "SmartDownloadThread";  
  18.     private File saveFile;  
  19.     private URL downUrl;  
  20.     private int block;  
  21.     /*  *下载开始位置 */  
  22.     private int threadId = -1;  
  23.     private int downLength;  
  24.     private boolean finish = false;  
  25.     private SmartFileDownloader downloader;  
  26.   
  27.     public SmartDownloadThread(SmartFileDownloader downloader, URL downUrl,  
  28.             File saveFile, int block, int downLength, int threadId) {  
  29.         this.downUrl = downUrl;  
  30.         this.saveFile = saveFile;  
  31.         this.block = block;  
  32.         this.downloader = downloader;  
  33.         this.threadId = threadId;  
  34.         this.downLength = downLength;  
  35.     }  
  36.   
  37.     @Override  
  38.     public void run() {  
  39.         if (downLength < block) {// 未下载完成  
  40.             try {  
  41.                 HttpURLConnection http = (HttpURLConnection) downUrl  
  42.                         .openConnection();  
  43.                 http.setConnectTimeout(5 * 1000);  
  44.                 http.setRequestMethod("GET");  
  45.                 http.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, */*");  
  46.                 http.setRequestProperty("Accept-Language", "zh-CN");  
  47.                 http.setRequestProperty("Referer", downUrl.toString());  
  48.                 http.setRequestProperty("Charset", "UTF-8");  
  49.                 int startPos = block * (threadId - 1) + downLength;// 开始位置  
  50.                 int endPos = block * threadId - 1;// 结束位置  
  51.                 http.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos);// 设置获取实体数据的范围  
  52.                 http.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");  
  53.                 http.setRequestProperty("Connection", "Keep-Alive");  
  54.   
  55.                 InputStream inStream = http.getInputStream();  
  56.                 byte[] buffer = new byte[1024];  
  57.                 int offset = 0;  
  58.                 print("Thread " + this.threadId + " start download from position " + startPos);  
  59.                 RandomAccessFile threadfile = new RandomAccessFile(this.saveFile, "rwd");  
  60.                 threadfile.seek(startPos);  
  61.                 while ((offset = inStream.read(buffer, 0, 1024)) != -1) {  
  62.                     threadfile.write(buffer, 0, offset);  
  63.                     downLength += offset;  
  64.                     downloader.update(this.threadId, downLength);  
  65.                     downloader.saveLogFile();  
  66.                     downloader.append(offset);  
  67.                 }  
  68.                 threadfile.close();  
  69.                 inStream.close();  
  70.                 print("Thread " + this.threadId + " download finish");  
  71.                 this.finish = true;  
  72.             } catch (Exception e) {  
  73.                 this.downLength = -1;  
  74.                 print("Thread " + this.threadId + ":" + e);  
  75.             }  
  76.         }  
  77.     }  
  78.   
  79.     private static void print(String msg) {  
  80.         Log.i(TAG, msg);  
  81.     }  
  82.   
  83.     /**  
  84.      * 下载是否完成  
  85.      * @return  
  86.      */  
  87.     public boolean isFinish() {  
  88.         return finish;  
  89.     }  
  90.   
  91.     /** 
  92.      * 已经下载的内容大小 
  93.      * @return 如果返回值为-1,代表下载失败 
  94.      */  
  95.     public long getDownLength() {  
  96.         return downLength;  
  97.     }  
  98. }  

 总得下载线程

SmartFileDownloader.java

 

Java代码  技术分享
    1. package com.hao.download;  
    2.   
    3. import java.io.File;  
    4. import java.io.RandomAccessFile;  
    5. import java.net.HttpURLConnection;  
    6. import java.net.URL;  
    7. import java.util.LinkedHashMap;  
    8. import java.util.Map;  
    9. import java.util.UUID;  
    10. import java.util.concurrent.ConcurrentHashMap;  
    11. import java.util.regex.Matcher;  
    12. import java.util.regex.Pattern;  
    13.   
    14. import com.hao.db.DownloadFileService;  
    15.   
    16. import android.content.Context;  
    17. import android.util.Log;  
    18.   
    19. /** 
    20.  * 文件下载主程序 
    21.  * @author Administrator 
    22.  *  
    23.  */  
    24. public class SmartFileDownloader {  
    25.     private static final String TAG = "SmartFileDownloader";  
    26.     private Context context;  
    27.     private DownloadFileService fileService;  
    28.     /* 已下载文件长度 */  
    29.     private int downloadSize = 0;  
    30.     /* 原始文件长度 */  
    31.     private int fileSize = 0;  
    32.     /*原始文件名*/  
    33.     private String fileName;  
    34.     /* 线程数 */  
    35.     private SmartDownloadThread[] threads;  
    36.     /* 本地保存文件 */  
    37.     private File saveFile;  
    38.     /* 缓存各线程下载的长度 */  
    39.     private Map<Integer, Integer> data = new ConcurrentHashMap<Integer, Integer>();  
    40.     /* 每条线程下载的长度 */  
    41.     private int block;  
    42.     /* 下载路径 */  
    43.     private String downloadUrl;  
    44.   
    45.     /** 
    46.      * 获取文件名 
    47.      */  
    48.     public String getFileName(){  
    49.         return this.fileName;  
    50.     }  
    51.     /** 
    52.      * 获取线程数 
    53.      */  
    54.     public int getThreadSize() {  
    55.         return threads.length;  
    56.     }  
    57.   
    58.     /** 
    59.      * 获取文件大小 
    60.      * @return 
    61.      */  
    62.     public int getFileSize() {  
    63.         return fileSize;  
    64.     }  
    65.   
    66.     /** 
    67.      * 累计已下载大小 
    68.      * @param size 
    69.      */  
    70.     protected synchronized void append(int size) {  
    71.         downloadSize += size;  
    72.     }  
    73.   
    74.     /** 
    75.      * 更新指定线程最后下载的位置 
    76.      * @param threadId 线程id 
    77.      * @param pos 最后下载的位置 
    78.      */  
    79.     protected void update(int threadId, int pos) {  
    80.         this.data.put(threadId, pos);  
    81.     }  
    82.   
    83.     /** 
    84.      * 保存记录文件 
    85.      */  
    86.     protected synchronized void saveLogFile() {  
    87.         this.fileService.update(this.downloadUrl, this.data);  
    88.     }  
    89.   
    90.     /** 
    91.      * 构建文件下载器 
    92.      * @param downloadUrl 下载路径 
    93.      * @param fileSaveDir 文件保存目录 
    94.      * @param threadNum 下载线程数 
    95.      */  
    96.     public SmartFileDownloader(Context context, String downloadUrl,  
    97.             File fileSaveDir, int threadNum) {  
    98.         try {  
    99.             this.context = context;  
    100.             this.downloadUrl = downloadUrl;  
    101.             fileService = new DownloadFileService(this.context);  
    102.             URL url = new URL(this.downloadUrl);  
    103.             if (!fileSaveDir.exists()) fileSaveDir.mkdirs();  
    104.             this.threads = new SmartDownloadThread[threadNum];  
    105.             HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
    106.             conn.setConnectTimeout(5 * 1000);  
    107.             conn.setRequestMethod("GET");  
    108.             conn.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, */*");  
    109.             conn.setRequestProperty("Accept-Language", "zh-CN");  
    110.             conn.setRequestProperty("Referer", downloadUrl);  
    111.             conn.setRequestProperty("Charset", "UTF-8");  
    112.             conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");  
    113.             conn.setRequestProperty("Connection", "Keep-Alive");  
    114.             conn.connect();  
    115.             printResponseHeader(conn);  
    116.             if (conn.getResponseCode() == 200) {  
    117.                 this.fileSize = conn.getContentLength();// 根据响应获取文件大小  
    118.                 if (this.fileSize <= 0)  
    119.                     throw new RuntimeException("Unkown file size ");  
    120.   
    121.                 fileName = getFileName(conn);  
    122.                 this.saveFile = new File(fileSaveDir, fileName);/* 保存文件 */  
    123.                 Map<Integer, Integer> logdata = fileService.getData(downloadUrl);  
    124.                 if (logdata.size() > 0) {  
    125.                     for (Map.Entry<Integer, Integer> entry : logdata.entrySet())  
    126.                         data.put(entry.getKey(), entry.getValue());  
    127.                 }  
    128.                 //划分每个线程下载文件长度  
    129.                 this.block = (this.fileSize % this.threads.length) == 0 ? this.fileSize / this.threads.length  
    130.                         : this.fileSize / this.threads.length + 1;  
    131.                 if (this.data.size() == this.threads.length) {  
    132.                     for (int i = 0; i < this.threads.length; i++) {  
    133.                         this.downloadSize += this.data.get(i + 1);  
    134.                     }  
    135.                     print("已经下载的长度" + this.downloadSize);  
    136.                 }  
    137.             } else {  
    138.                 throw new RuntimeException("server no response ");  
    139.             }  
    140.         } catch (Exception e) {  
    141.             print(e.toString());  
    142.             throw new RuntimeException("don‘t connection this url");  
    143.         }  
    144.     }  
    145.   
    146.     /** 
    147.      * 获取文件名 
    148.      */  
    149.     private String getFileName(HttpURLConnection conn) {  
    150.         String filename = this.downloadUrl.substring(this.downloadUrl.lastIndexOf(‘/‘) + 1);//链接的最后一个/就是文件名  
    151.         if (filename == null || "".equals(filename.trim())) {// 如果获取不到文件名称  
    152.             for (int i = 0;; i++) {  
    153.                 String mine = conn.getHeaderField(i);  
    154.                 print("ConnHeader:"+mine+" ");  
    155.                 if (mine == null)  
    156.                     break;  
    157.                 if ("content-disposition".equals(conn.getHeaderFieldKey(i).toLowerCase())) {  
    158.                     Matcher m = Pattern.compile(".*filename=(.*)").matcher(mine.toLowerCase());  
    159.                     if (m.find())  
    160.                         return m.group(1);  
    161.                 }  
    162.             }  
    163.             filename = UUID.randomUUID() + ".tmp";// 默认取一个文件名  
    164.         }  
    165.         return filename;  
    166.     }  
    167.   
    168.     /** 
    169.      * 开始下载文件 
    170.      *  
    171.      * @param listener 
    172.      *            监听下载数量的变化,如果不需要了解实时下载的数量,可以设置为null 
    173.      * @return 已下载文件大小 
    174.      * @throws Exception 
    175.      */  
    176.     public int download(SmartDownloadProgressListener listener)  
    177.             throws Exception {  
    178.         try {  
    179.             RandomAccessFile randOut = new RandomAccessFile(this.saveFile, "rw");  
    180.             if (this.fileSize > 0)  
    181.                 randOut.setLength(this.fileSize);  
    182.             randOut.close();  
    183.             URL url = new URL(this.downloadUrl);  
    184.             if (this.data.size() != this.threads.length) {  
    185.                 this.data.clear();// 清除数据  
    186.                 for (int i = 0; i < this.threads.length; i++) {  
    187.                     this.data.put(i + 1, 0);  
    188.                 }  
    189.             }  
    190.             for (int i = 0; i < this.threads.length; i++) {  
    191.                 int downLength = this.data.get(i + 1);  
    192.                 if (downLength < this.block && this.downloadSize < this.fileSize) { // 该线程未完成下载时,继续下载  
    193.                     this.threads[i] = new SmartDownloadThread(this, url,  
    194.                             this.saveFile, this.block, this.data.get(i + 1), i + 1);  
    195.                     this.threads[i].setPriority(7);  
    196.                     this.threads[i].start();  
    197.                 } else {  
    198.                     this.threads[i] = null;  
    199.                 }  
    200.             }  
    201.             this.fileService.save(this.downloadUrl, this.data);  
    202.             boolean notFinish = true;// 下载未完成  
    203.             while (notFinish) {// 循环判断是否下载完毕  
    204.                 Thread.sleep(900);  
    205.                 notFinish = false;// 假定下载完成  
    206.                 for (int i = 0; i < this.threads.length; i++) {  
    207.                     if (this.threads[i] != null && !this.threads[i].isFinish()) {  
    208.                         notFinish = true;// 下载没有完成  
    209.                         if (this.threads[i].getDownLength() == -1) {// 如果下载失败,再重新下载  
    210.                             this.threads[i] = new SmartDownloadThread(this,  
    211.                                     url, this.saveFile, this.block, this.data.get(i + 1), i + 1);  
    212.                             this.threads[i].setPriority(7);  
    213.                             this.threads[i].start();  
    214.                         }  
    215.                     }  
    216.                 }  
    217.                 if (listener != null)  
    218.                     listener.onDownloadSize(this.downloadSize);  
    219.             }  
    220.             fileService.delete(this.downloadUrl);  
    221.         } catch (Exception e) {  
    222.             print(e.toString());  
    223.             throw new Exception("file download fail");  
    224.         }  
    225.         return this.downloadSize;  
    226.     }  
    227.   
    228.     /** 
    229.      * 获取Http响应头字段 
    230.      *  
    231.      * @param http 
    232.      * @return 
    233.      */  
    234.     public static Map<String, String> getHttpResponseHeader(  
    235.             HttpURLConnection http) {  
    236.         Map<String, String> header = new LinkedHashMap<String, String>();  
    237.         for (int i = 0;; i++) {  
    238.             String mine = http.getHeaderField(i);  
    239.             if (mine == null)  
    240.                 break;  
    241.             header.put(http.getHeaderFieldKey(i), mine);  
    242.         }  
    243.         return header;  
    244.     }  
    245.   
    246.     /** 
    247.      * 打印Http头字段 
    248.      *  
    249.      * @param http 
    250.      */  
    251.     public static void printResponseHeader(HttpURLConnection http) {  
    252.         Map<String, String> header = getHttpResponseHeader(http);  
    253.         for (Map.Entry<String, String> entry : header.entrySet()) {  
    254.             String key = entry.getKey() != null ? entry.getKey() + ":" : "";  
    255.             print(key + entry.getValue());  
    256.         }  
    257.     }  
    258.   
    259.     // 打印日志  
    260.     private static void print(String msg) {  
    261.         Log.i(TAG, msg);  
    262.     }  
    263.   
    264.     public interface SmartDownloadProgressListener {  
    265.         public void onDownloadSize(int size);  
    266.     }  
    267. }  

 

 

代码实现 参考 http://hao3100590.iteye.com/blog/1295903

 

断点续传和下载原理分析

标签:

原文地址:http://www.cnblogs.com/wangzehuaw/p/5610851.html

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