斷點下載,android斷點下載

來源:互聯網
上載者:User

斷點下載,android斷點下載

 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 @end16 17 @implementation ViewController18 - (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---delegate51 // 收到響應52 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response53 {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 *)data68 {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 *)connection80 {81     NSLog(@"下載完畢");82 }83 // 更新progress84 - (void)updateProgress85 {86     self.progress.progress = (long double)_current / (long double)_total;87 }88 89 @end

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.