zoukankan      html  css  js  c++  java
  • 断点下载

     1 @interface ViewController ()<NSURLConnectionDataDelegate>
     2 {
     3     // 文件的总大小
     4     long long _total;
     5     // 当前下载
     6     long long _current;
     7 }
     8 
     9 // 文件句柄
    10 @property (nonatomic,strong)NSFileHandle *fileHandle;
    11 // 连接对象
    12 @property (nonatomic,strong)NSURLConnection *conn;
    13 @property (weak, nonatomic) IBOutlet UIProgressView *progress;
    14 @property (weak, nonatomic) IBOutlet UIButton *button;
    15 @end
    16 
    17 @implementation ViewController
    18 - (void)viewDidLoad {
    19     [super viewDidLoad];
    20     self.progress.progress = 0;
    21     // Do any additional setup after loading the view, typically from a nib.
    22 }
    23 
    24 - (IBAction)buttonAction:(UIButton *)sender {
    25     if ([[sender titleForState:UIControlStateNormal]isEqualToString:@"开始"]) {
    26         [sender setTitle:@"暂停" forState:(UIControlStateNormal)];
    27         // 请求
    28         NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:kUrl]];
    29         // 设置请求参数
    30         NSString *rang = [NSString stringWithFormat:@"byte:%lld",_current];
    31         NSLog(@"request:%@end",request);
    32         [request setValue:rang forHTTPHeaderField:@"Range"];
    33          NSLog(@"request:%@end",request);
    34         // 建立连接
    35         self.conn = [NSURLConnection connectionWithRequest:request delegate:self];
    36         // 开始请求
    37         [_conn start];
    38     }else{
    39         [sender setTitle:@"开始" forState:(UIControlStateNormal)];
    40         // 取消
    41         [_conn cancel];
    42     }
    43 }
    44 
    45 - (void)didReceiveMemoryWarning {
    46     [super didReceiveMemoryWarning];
    47     // Dispose of any resources that can be recreated.
    48 }
    49 
    50 #pragma mark---delegate
    51 // 收到响应
    52 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    53 {
    54     // 获取总文件大小(总文件的大小在响应里面可以获得)
    55     _total = response.expectedContentLength;
    56     // 创建文件
    57     NSFileManager *manager = [NSFileManager defaultManager];
    58     NSString *cachesPath  = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    59     NSString *filePath = [cachesPath stringByAppendingPathComponent:@"music.mp3"];
    60     NSLog(@"%@",cachesPath);
    61     // 创建文件
    62     [manager createFileAtPath:filePath contents:nil attributes:nil];
    63     // 让文件句柄指向文件
    64     self.fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
    65 }
    66 // 接收到数据
    67 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    68 {
    69     // 计算当前总大小
    70     _current += data.length;
    71     // 更新 (在子线程中不能更新UI)
    72     [self performSelectorOnMainThread:@selector(updateProgress) withObject:self waitUntilDone:YES];
    73     // 指向文件末尾
    74     [self.fileHandle seekToEndOfFile];
    75     // 写数据
    76     [self.fileHandle writeData:data];
    77 }
    78 // 结束
    79 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    80 {
    81     NSLog(@"下载完毕");
    82 }
    83 // 更新progress
    84 - (void)updateProgress
    85 {
    86     self.progress.progress = (long double)_current / (long double)_total;
    87 }
    88 
    89 @end
  • 相关阅读:
    org.apache.hadoop.security.AccessControlException: org.apache.hadoop.security.AccessControlException: Permission denied:
    linux iptables跟service 命令无效解决方法
    eclipse运行C++控制台不输出结果的解决办法
    C#操作mpp文件代码参考
    vs2010 开始执行(不调试) ctrl F5 出现任意键继续
    关于gcc、glibc和binutils模块之间的关系,以及在现有系统上如何升级的总结
    NUnit在Visual Studio 2010中的配置和使用
    【转】从CSDN的趣味题学C# 3.0
    gnome3程序图标下的文字如何改大
    使用autoconf、automake等来制作一个以源代码形式(.tar.gz)发布的软件、并可在执行configure时使用自定义参数
  • 原文地址:https://www.cnblogs.com/cityingma/p/4920013.html
Copyright © 2011-2022 走看看