zoukankan      html  css  js  c++  java
  • 下载器

    //
    //  ViewController.m
    //  NSConnection-0805
    //
    //  Created by apple on 14-8-5.
    //  Copyright (c) 2014年 apple. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()<NSURLConnectionDataDelegate>
    {
        NSMutableData *_totalData;
        long long _totalDataLenth;
        NSFileHandle *_fileHandle;
        NSURL *_url;
        NSURLConnection *_connection;
    }
    @property(nonatomic,strong)UIProgressView *progressView;
    @property(nonatomic,strong)UITextField *urlTextField;
    @end
    
    @implementation ViewController
                
    - (void)viewDidLoad {
        [super viewDidLoad];
        _totalData=[[NSMutableData alloc]init];
        self.view.backgroundColor=[UIColor greenColor];
        //进度条的创建
        _progressView=[[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
        _progressView.frame=CGRectMake(70, 150, 150, 20);
        _progressView.progressTintColor=[UIColor blueColor];
        [self.view addSubview:_progressView];
        
        //URL输入栏
        _urlTextField=[[UITextField alloc]initWithFrame:CGRectMake(40, 200, 200, 30)];
        _urlTextField.backgroundColor=[UIColor whiteColor];
        [self.view addSubview:_urlTextField];
        
        //创建下载按钮
        UIButton *downLoad=[UIButton buttonWithType:UIButtonTypeCustom];
        downLoad.frame=CGRectMake(100, 300, 120, 40);
        [downLoad setTitle:@"开始下载" forState:UIControlStateNormal];
        [downLoad addTarget:self action:@selector(down) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:downLoad];
        
        
        NSFileManager *fileManager=[NSFileManager defaultManager];
        [fileManager createFileAtPath:@"/Users/apple/Desktop/test1.mp4" contents:nil attributes:nil];
        _fileHandle=[NSFileHandle fileHandleForWritingAtPath:@"/Users/apple/Desktop/test1.mp4"] ;
    //    _url=[NSURL URLWithString:@"http://class.room/hdmv.mp4"];
    
    //    NSURLRequest *request=[NSURLRequest requestWithURL:_url];
    //    NSURLConnection *connection=[NSURLConnection connectionWithRequest:request delegate:self];
    //    [connection start];
    //    _connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];
        
    }
    
    //按钮方法
    - (void)down
    {
        
        [_fileHandle truncateFileAtOffset:0];
        _url=[NSURL URLWithString:_urlTextField.text];
        NSURLRequest *request=[NSURLRequest requestWithURL:_url];
        //    NSURLConnection *connection=[NSURLConnection connectionWithRequest:request delegate:self];
        //    [connection start];
        _connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];
        //开始下载
        [_connection start];
    //    [_connection cancel];
    }
    
    //连接失败响应方法
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
        NSLog(@"connection fail %@",error);
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
        NSLog(@"Response%@",response);
        _totalDataLenth=[response expectedContentLength];
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        NSLog(@"接收数据");
    //    [_totalData appendData:data];
        [_fileHandle writeData:data];
    //    NSLog(@"==%d",[_totalData length]);
        NSLog(@"data %f",(float)[_fileHandle offsetInFile]/_totalDataLenth);
        _progressView.progress=(float)[_fileHandle offsetInFile]/_totalDataLenth;
    }
    
    //连接完成
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        NSLog(@"connection finished");
        [_fileHandle closeFile];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
  • 相关阅读:
    sqlserver调优-索引
    浮点数的这些坑,你未必知道-深入理解浮点数的规律
    生产环境部署springcloud微服务启动慢的问题排查
    redis传输协议规范-下(Redis Protocol specification)
    一步步完成“迷你版” 的ASP.NET Core框架
    安全漏洞整改系列(一)
    docker实战(二)之redis的使用
    docker实战(一)之Tomcat的安装
    docker安装步骤
    WPF后台操作前台元素之查找对象
  • 原文地址:https://www.cnblogs.com/lidongq/p/3893632.html
Copyright © 2011-2022 走看看