iOS Downloader斷點續(xù)傳

1. 最簡單的方式

1). 創(chuàng)建一個下載工具類MTDownloader
MTDownloader.h文件

#import <Foundation/Foundation.h>

@interface MTDownloader : NSObject

- (void)download:(NSString *)netUrl;

@end

MTDownloader.m文件

// 下載运沦,傳入下載鏈接
- (void)download:(NSString *)netUrl {
    NSURL *url = [NSURL URLWithString:netUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:
     ^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
         if (connectionError) {
             NSLog(@"連接錯誤 %@", connectionError);
             return;
         }
         NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
         if (httpResponse.statusCode == 200 || httpResponse.statusCode == 304) {
             // 解析數(shù)據(jù)
             // 存在的問題,內(nèi)存暴漲动壤,無法獲取下載進(jìn)度
             [data writeToFile:@"/Users/mazaiting/Desktop/1.zip" atomically:YES];
             NSLog(@"下載成功");
         } else {
             NSLog(@"服務(wù)器內(nèi)部錯誤");
         }
     }];
}

這種下載方式存在的問題是下載時內(nèi)存暴漲盯质,因為文件讀取到內(nèi)存中挺据,并且無法獲取下載進(jìn)度。
2). 在ViewController.m文件中使用

#import "ViewController.h"
#import "MTDownloader.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

// 觸摸屏幕時
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    MTDownloader *downloader = [MTDownloader new];
    [downloader download:@"http://127.0.0.1/HBuilder.zip"];
}

@end

2. 解決獲取下載進(jìn)度問題

改造MTDownloader.m文件


#import "MTDownloader.h"
// 分類
@interface MTDownloader () <NSURLConnectionDataDelegate>
// 記錄文件的總長度,因為在獲取下載進(jìn)度的時候需要文件總長度,所以定義為屬性
@property (nonatomic, assign) long long expectedContentLength;
// 記錄當(dāng)前文件的長度
@property (nonatomic, assign) long long currentFileSize;
// 用來記錄總共下載的數(shù)據(jù)
@property (nonatomic, strong) NSMutableData *mutableData;
@end

@implementation MTDownloader

// 懶加載
- (NSMutableData *)mutableData {
    if (_mutableData == nil) {
        _mutableData = [NSMutableData data];
    }
    return _mutableData;
}

- (void)download:(NSString *)netUrl {
    // 創(chuàng)建url
    NSURL *url = [NSURL URLWithString:netUrl];
    // 創(chuàng)建下載請求
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    // 設(shè)置代理, 實現(xiàn)NSURLConnectionDataDelegate協(xié)議
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

#pragma mark- 代理方法
// 接收到響應(yīng)頭
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"接收到響應(yīng) %@", response);
    // 獲取到文件的總長度
    self.expectedContentLength = response.expectedContentLength;
}

// 接收到數(shù)據(jù)--一點點接收
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    // 將每次接收到的字節(jié)長度與已有的文件長度相加
    self.currentFileSize += data.length;
    // 添加接收的數(shù)據(jù)
    [self.mutableData appendData:data];
    // 當(dāng)前已下載的文件長度 * 1.0 / 文件的總長度 = 當(dāng)前的進(jìn)度
    float process = self.currentFileSize * 1.0 / self.expectedContentLength;
    NSLog(@"接收數(shù)據(jù): %f", process);
}

// 下載完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"下載完成");
    // 下載完成后將字節(jié)數(shù)組寫入文件
    [self.mutableData writeToFile:@"/Users/mazaiting/Desktop/1.zip" atomically:YES];
}

// 下載出錯
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"下載出錯");
}

@end

改造完成后蒸其,可以獲取下載文件的進(jìn)度,但仍然后出現(xiàn)內(nèi)存暴漲的問題库快。

3. 解決文件內(nèi)存暴漲--NSFileHandle/NSOutputStream

將第二步中記錄下載數(shù)據(jù)的mutableData屬性刪除摸袁,在此處不再需要,因為是將文件一點一點的寫入义屏。
在接收一點點接收數(shù)據(jù)的地方進(jìn)行數(shù)據(jù)的存儲靠汁。

1). NSFileHandle解決

#import "MTDownloader.h"
// 分類
@interface MTDownloader () <NSURLConnectionDataDelegate>
// 記錄文件的總長度,因為在獲取下載進(jìn)度的時候需要文件總長度,所以定義為屬性
@property (nonatomic, assign) long long expectedContentLength;
// 記錄當(dāng)前文件的長度
@property (nonatomic, assign) long long currentFileSize;
@end

@implementation MTDownloader

- (void)download:(NSString *)netUrl {
    // 創(chuàng)建url
    NSURL *url = [NSURL URLWithString:netUrl];
    // 創(chuàng)建下載請求
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    // 設(shè)置代理, 實現(xiàn)NSURLConnectionDataDelegate協(xié)議
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

#pragma mark- 代理方法
// 接收到響應(yīng)頭
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"接收到響應(yīng) %@", response);
    // 獲取到文件的總長度
    self.expectedContentLength = response.expectedContentLength;
}

// 接收到數(shù)據(jù)--一點點接收
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    // 將每次接收到的字節(jié)長度與已有的文件長度相加
    self.currentFileSize += data.length;
    // 當(dāng)前已下載的文件長度 * 1.0 / 文件的總長度 = 當(dāng)前的進(jìn)度
    float process = self.currentFileSize * 1.0 / self.expectedContentLength;
    NSLog(@"接收數(shù)據(jù): %f", process);
    // 保存數(shù)據(jù)
    [self saveData:data];
}

// 下載完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"下載完成");
}

// 下載出錯
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"下載出錯");
}

// 保存數(shù)據(jù)
// data 每次下載的二進(jìn)制數(shù)據(jù)
- (void)saveData:(NSData *)data {
    // 保存文件的路徑
    NSString *filePath = @"/Users/mazaiting/Desktop/1.zip";
    // 根據(jù)路徑獲取NSFileHandle, 它不會自動創(chuàng)建文件闽铐,而是第0個字節(jié)添加數(shù)據(jù)
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
    // 如果fileHandle存在蝶怔,則讓fileHandle的offset偏移量指向文件的末尾,進(jìn)行數(shù)據(jù)的添加
    // 如果fileHandle不存在兄墅,則創(chuàng)建文件
    if (fileHandle) {
        // fileHandle存在
        // 將文件指針指向文件的末尾
        [fileHandle seekToEndOfFile];
        // 寫數(shù)據(jù)
        [fileHandle writeData:data];
        // 關(guān)閉文件
        [fileHandle closeFile];
    } else {
        // fileHandle不存在
        [data writeToFile:filePath atomically:YES];
    }
}

@end

2). NSOutputStream解決
流操作時要注意打開流踢星,關(guān)閉流,并且要注意初始化流的位置隙咸。文件寫入時推薦使用流操作沐悦。

#import "MTDownloader.h"
// 分類
@interface MTDownloader () <NSURLConnectionDataDelegate>
// 記錄文件的總長度,因為在獲取下載進(jìn)度的時候需要文件總長度,所以定義為屬性
@property (nonatomic, assign) long long expectedContentLength;
// 記錄當(dāng)前文件的長度
@property (nonatomic, assign) long long currentFileSize;
// NSOutputStream用來保存每次下載的二進(jìn)制數(shù)據(jù)
@property (nonatomic, strong) NSOutputStream *outputStream;
@end

@implementation MTDownloader

- (void)download:(NSString *)netUrl {
    // 創(chuàng)建url
    NSURL *url = [NSURL URLWithString:netUrl];
    // 創(chuàng)建下載請求
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    // 設(shè)置代理, 實現(xiàn)NSURLConnectionDataDelegate協(xié)議
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

#pragma mark- 代理方法
// 接收到響應(yīng)頭
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"接收到響應(yīng) %@", response);
    // 獲取到文件的總長度
    self.expectedContentLength = response.expectedContentLength;
    // 保存文件的路徑
    NSString *filePath = @"/Users/mazaiting/Desktop/1.zip";
    // 接收到響應(yīng)頭的時候初始化outputStream
    self.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:YES];
    // 打開流
    [self.outputStream open];
}

// 接收到數(shù)據(jù)--一點點接收
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    // 將每次接收到的字節(jié)長度與已有的文件長度相加
    self.currentFileSize += data.length;
    // 當(dāng)前已下載的文件長度 * 1.0 / 文件的總長度 = 當(dāng)前的進(jìn)度
    float process = self.currentFileSize * 1.0 / self.expectedContentLength;
    NSLog(@"接收數(shù)據(jù): %f", process);
    // 保存數(shù)據(jù)
    [self saveData:data];
}

// 下載完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"下載完成");
    // 下載完成的時候扎瓶,關(guān)閉流
    [self.outputStream close];}

// 下載出錯
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"下載出錯");
    // 判斷流是否為空所踊,下載出錯的時候,關(guān)閉流
    if (self.outputStream) {
        [self.outputStream close];
    }
}

// NSOutputStream保存數(shù)據(jù)
// data 每次下載的二進(jìn)制文件
- (void)saveData:(NSData *)data {
    // 寫數(shù)據(jù)
    [self.outputStream write:data.bytes maxLength:data.length];
}

// NSFileHandle保存數(shù)據(jù)
// data 每次下載的二進(jìn)制數(shù)據(jù)
- (void)saveData1:(NSData *)data {
    // 保存文件的路徑
    NSString *filePath = @"/Users/mazaiting/Desktop/1.zip";
    // 根據(jù)路徑獲取NSFileHandle, 它不會自動創(chuàng)建文件概荷,而是第0個字節(jié)添加數(shù)據(jù)
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
    // 如果fileHandle存在秕岛,則讓fileHandle的offset偏移量指向文件的末尾,進(jìn)行數(shù)據(jù)的添加
    // 如果fileHandle不存在误证,則創(chuàng)建文件
    if (fileHandle) {
        // fileHandle存在
        // 將文件指針指向文件的末尾
        [fileHandle seekToEndOfFile];
        // 寫數(shù)據(jù)
        [fileHandle writeData:data];
        // 關(guān)閉文件
        [fileHandle closeFile];
    } else {
        // fileHandle不存在
        [data writeToFile:filePath atomically:YES];
    }
}

@end

此時继薛,一個下載的操作工具類就已完成,但目前無法使用斷點下載愈捅。

4. 文件的斷點下載操作

斷點續(xù)傳思路
I. 首先發(fā)送一個head請求遏考,獲取到服務(wù)器文件的大小和名稱
II. 判斷本地文件是否存在
1). 不存在,從0開始下載
2). 存在
a. 本地文件大小 == 服務(wù)器文件大小 不需要下載
b. 本地文件大小 < 服務(wù)器文件大小 從當(dāng)前位置下載
c. 本地文件打下 > 服務(wù)器文件大小 刪除文件蓝谨,從0下載

//
// 斷點續(xù)傳思路
// 1. 首先發(fā)送一個head請求灌具,獲取到服務(wù)器文件的大小和名稱
// 2. 判斷本地文件是否存在
//      1). 不存在青团,從0開始下載
//      2). 存在
//          a. 本地文件大小 == 服務(wù)器文件大小  不需要下載
//          b. 本地文件大小 < 服務(wù)器文件大小   從當(dāng)前位置下載
//          c. 本地文件打下 > 服務(wù)器文件大小   刪除文件,從0下載
#import "MTDownloader.h"
// 分類
@interface MTDownloader () <NSURLConnectionDataDelegate>
// 記錄文件的總長度,因為在獲取下載進(jìn)度的時候需要文件總長度咖楣,所以定義為屬性
@property (nonatomic, assign) long long expectedContentLength;
// 記錄當(dāng)前文件的長度
@property (nonatomic, assign) long long currentFileSize;
// NSOutputStream用來保存每次下載的二進(jìn)制數(shù)據(jù)
@property (nonatomic, strong) NSOutputStream *outputStream;
// 文件的存儲路徑
@property (nonatomic, strong) NSString *filePath;
@end

@implementation MTDownloader

- (void)download:(NSString *)netUrl {
    // 創(chuàng)建url
    NSURL *url = [NSURL URLWithString:netUrl];
    // 獲取服務(wù)器文件信息, 獲取當(dāng)前文件長度
    [self getServerFileInfo:url];
    // 獲取本地文件信息督笆,并且和服務(wù)器做比較
    self.currentFileSize = [self getLocalFileInfo];
    // 判斷當(dāng)前文件大小是否是-1,如果是-1诱贿,說明文件已經(jīng)下載完成
    if (self.currentFileSize == -1) {
        NSLog(@"文件已下載完成娃肿,請不要重復(fù)下載!!!");
        return;
    }
    
    // 從指定位置處下載文件
    [self downloadFile: url];
}

// 從指定位置處下載文件
- (void)downloadFile:(NSURL *)url {
    // 創(chuàng)建一個請求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    // 請求頭
    // Range:bytes=x-y 從x個字節(jié)下載到y(tǒng)個字節(jié)
    // Range:bytes=x-  從x個字節(jié)下載到最后
    // Range:bytes=-y  從開始下載到y(tǒng)個字節(jié)
    [request setValue:[NSString stringWithFormat:@"bytes=%lld-", self.currentFileSize] forHTTPHeaderField:@"Range"];
    
    // 設(shè)置代理, 實現(xiàn)NSURLConnectionDataDelegate協(xié)議
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

// 檢測本地文件大小
- (long long)getLocalFileInfo {
    // 文件的長度, 如果文件不存在珠十,則為0
    long long fileSize = 0;
    // 獲取文件管理者對象
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // 判斷文件是否存在
    if ([fileManager fileExistsAtPath:self.filePath]) {
        // 文件存在
        // 獲取本地文件的大小
        NSDictionary *attrs = [fileManager attributesOfItemAtPath:self.filePath error:NULL];
        // 賦值
        fileSize = attrs.fileSize;
        // 文件大小等于服務(wù)器文件長度
        if (fileSize == self.expectedContentLength) {
            fileSize = -1;
        }
        // 文件大小大于服務(wù)器文件長度
        if (fileSize > self.expectedContentLength) {
            fileSize = 0;
            // 刪除文件
            [fileManager removeItemAtPath:self.filePath error:NULL];
        }
    }
    return fileSize;
}

// 獲取服務(wù)器文件信息
// 文件的url
- (void)getServerFileInfo:(NSURL *)url {
    // 創(chuàng)建一個請求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    // 設(shè)置請求方法
    request.HTTPMethod = @"head";
    // 同步請求響應(yīng)體
    NSURLResponse *response = nil;
    // 發(fā)送一個同步請求
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];
    
    // 設(shè)置文件的總長度料扰,注意:在下載時- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;方法中為當(dāng)前文件總長度賦值的語句要刪除
    self.expectedContentLength = response.expectedContentLength;
    // 設(shè)置文件的存儲路徑, 存儲在臨時目錄下,拼接文件名
    self.filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:response.suggestedFilename];
    NSLog(@"%@", self.filePath);
}

#pragma mark- 代理方法
// 接收到響應(yīng)頭
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"接收到響應(yīng) %@", response);
    // 接收到響應(yīng)頭的時候初始化outputStream
    self.outputStream = [NSOutputStream outputStreamToFileAtPath:self.filePath append:YES];
    // 打開流
    [self.outputStream open];
}

// 接收到數(shù)據(jù)--一點點接收
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    // 將每次接收到的字節(jié)長度與已有的文件長度相加
    self.currentFileSize += data.length;
    // 當(dāng)前已下載的文件長度 * 1.0 / 文件的總長度 = 當(dāng)前的進(jìn)度
    float process = self.currentFileSize * 1.0 / self.expectedContentLength;
    NSLog(@"接收數(shù)據(jù): %f", process);
    // 保存數(shù)據(jù)
    [self saveData:data];
}

// 下載完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"下載完成");
    // 下載完成的時候焙蹭,關(guān)閉流
    [self.outputStream close];}

// 下載出錯
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"下載出錯");
    // 判斷流是否為空晒杈,下載出錯的時候,關(guān)閉流
    if (self.outputStream) {
        [self.outputStream close];
    }
}

// NSOutputStream保存數(shù)據(jù)
// data 每次下載的二進(jìn)制文件
- (void)saveData:(NSData *)data {
    // 寫數(shù)據(jù)
    [self.outputStream write:data.bytes maxLength:data.length];
}

// NSFileHandle保存數(shù)據(jù)
// data 每次下載的二進(jìn)制數(shù)據(jù)
- (void)saveData1:(NSData *)data {
    // 保存文件的路徑
    NSString *filePath = @"/Users/mazaiting/Desktop/1.zip";
    // 根據(jù)路徑獲取NSFileHandle, 它不會自動創(chuàng)建文件壳嚎,而是第0個字節(jié)添加數(shù)據(jù)
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
    // 如果fileHandle存在桐智,則讓fileHandle的offset偏移量指向文件的末尾,進(jìn)行數(shù)據(jù)的添加
    // 如果fileHandle不存在烟馅,則創(chuàng)建文件
    if (fileHandle) {
        // fileHandle存在
        // 將文件指針指向文件的末尾
        [fileHandle seekToEndOfFile];
        // 寫數(shù)據(jù)
        [fileHandle writeData:data];
        // 關(guān)閉文件
        [fileHandle closeFile];
    } else {
        // fileHandle不存在
        [data writeToFile:filePath atomically:YES];
    }
}

@end

5. 下載回調(diào)設(shè)置

MTDownloader.h 中在download方法中增加成功、進(jìn)度然磷、失敗的對調(diào)block郑趁。

#import <Foundation/Foundation.h>

@interface MTDownloader : NSObject
// 下載方法
// netUrl 下載的網(wǎng)絡(luò)連接
// successBlock 成功回調(diào)block
// processBlock 進(jìn)度對調(diào)block
// errorBlock 異常回調(diào)block
- (void)download:(NSString *)netUrl successBlock:(void(^)(NSString *path))successBlock processBlock:(void(^)(float process))processBlock errorBlock:(void(^)(NSError *error))errorBlock;

@end

MTDownloader.m 中實現(xiàn)該方法姿搜,并將下載文件的操作重新開啟一個新的線程來執(zhí)行下載任務(wù)寡润。

//
// 斷點續(xù)傳思路
// 1. 首先發(fā)送一個head請求,獲取到服務(wù)器文件的大小和名稱
// 2. 判斷本地文件是否存在
//      1). 不存在舅柜,從0開始下載
//      2). 存在
//          a. 本地文件大小 == 服務(wù)器文件大小  不需要下載
//          b. 本地文件大小 < 服務(wù)器文件大小   從當(dāng)前位置下載
//          c. 本地文件打下 > 服務(wù)器文件大小   刪除文件梭纹,從0下載
#import "MTDownloader.h"
// 分類
@interface MTDownloader () <NSURLConnectionDataDelegate>
// 記錄文件的總長度,因為在獲取下載進(jìn)度的時候需要文件總長度,所以定義為屬性
@property (nonatomic, assign) long long expectedContentLength;
// 記錄當(dāng)前文件的長度
@property (nonatomic, assign) long long currentFileSize;
// NSOutputStream用來保存每次下載的二進(jìn)制數(shù)據(jù)
@property (nonatomic, strong) NSOutputStream *outputStream;
// 文件的存儲路徑
@property (nonatomic, strong) NSString *filePath;
// 下載成功后的回調(diào)
@property (nonatomic, copy) void (^successBlock)(NSString *path);
// 下載進(jìn)度回調(diào)
@property (nonatomic, copy) void (^processBlock)(float process);
// 下載異常的回調(diào)
@property (nonatomic, copy) void (^errorBlock)(NSError *error);
@end

@implementation MTDownloader
// 下載
- (void)download:(NSString *)netUrl successBlock:(void (^)(NSString *))successBlock processBlock:(void (^)(float))processBlock errorBlock:(void (^)(NSError *))errorBlock {
    self.successBlock = successBlock;
    self.processBlock = processBlock;
    self.errorBlock = errorBlock;
    
    // 創(chuàng)建url
    NSURL *url = [NSURL URLWithString:netUrl];
    // 獲取服務(wù)器文件信息, 獲取當(dāng)前文件長度
    [self getServerFileInfo:url];
    // 獲取本地文件信息致份,并且和服務(wù)器做比較
    self.currentFileSize = [self getLocalFileInfo];
    // 判斷當(dāng)前文件大小是否是-1变抽,如果是-1,說明文件已經(jīng)下載完成
    if (self.currentFileSize == -1) {
//        NSLog(@"文件已下載完成氮块,請不要重復(fù)下載!!!");
        // 如果下載回調(diào)不為空
        if (self.successBlock) {
            // 分發(fā)消息到主線程绍载,讓主線程來處理
            dispatch_async(dispatch_get_main_queue(), ^{
                self.successBlock(self.filePath);
            });
        }
        return;
    }
    
    // 從指定位置處下載文件
    [self downloadFile: url];
}

// 從指定位置處下載文件
- (void)downloadFile:(NSURL *)url {
    // 創(chuàng)建一個新的線程,并添加到隊列中
    [[NSOperationQueue new] addOperationWithBlock:^{
        // 創(chuàng)建一個請求
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        // 請求頭
        // Range:bytes=x-y 從x個字節(jié)下載到y(tǒng)個字節(jié)
        // Range:bytes=x-  從x個字節(jié)下載到最后
        // Range:bytes=-y  從開始下載到y(tǒng)個字節(jié)
        [request setValue:[NSString stringWithFormat:@"bytes=%lld-", self.currentFileSize] forHTTPHeaderField:@"Range"];
        
        // 設(shè)置代理, 實現(xiàn)NSURLConnectionDataDelegate協(xié)議
        [[NSURLConnection alloc] initWithRequest:request delegate:self];
        // 開啟消息循環(huán)
        [[NSRunLoop currentRunLoop] run];
    }];
    
}

// 檢測本地文件大小
- (long long)getLocalFileInfo {
    // 文件的長度滔蝉, 如果文件不存在击儡,則為0
    long long fileSize = 0;
    // 獲取文件管理者對象
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // 判斷文件是否存在
    if ([fileManager fileExistsAtPath:self.filePath]) {
        // 文件存在
        // 獲取本地文件的大小
        NSDictionary *attrs = [fileManager attributesOfItemAtPath:self.filePath error:NULL];
        // 賦值
        fileSize = attrs.fileSize;
        // 文件大小等于服務(wù)器文件長度
        if (fileSize == self.expectedContentLength) {
            fileSize = -1;
        }
        // 文件大小大于服務(wù)器文件長度
        if (fileSize > self.expectedContentLength) {
            fileSize = 0;
            // 刪除文件
            [fileManager removeItemAtPath:self.filePath error:NULL];
        }
    }
    return fileSize;
}

// 獲取服務(wù)器文件信息
// 文件的url
- (void)getServerFileInfo:(NSURL *)url {
    // 創(chuàng)建一個請求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    // 設(shè)置請求方法
    request.HTTPMethod = @"head";
    // 同步請求響應(yīng)體
    NSURLResponse *response = nil;
    // 發(fā)送一個同步請求
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];
    
    // 設(shè)置文件的總長度,注意:在下載時- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;方法中為當(dāng)前文件總長度賦值的語句要刪除
    self.expectedContentLength = response.expectedContentLength;
    // 設(shè)置文件的存儲路徑, 存儲在臨時目錄下蝠引,拼接文件名
    self.filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:response.suggestedFilename];
//    NSLog(@"%@", self.filePath);
}

#pragma mark- 代理方法
// 接收到響應(yīng)頭
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
//    NSLog(@"接收到響應(yīng) %@", response);
    // 接收到響應(yīng)頭的時候初始化outputStream
    self.outputStream = [NSOutputStream outputStreamToFileAtPath:self.filePath append:YES];
    // 打開流
    [self.outputStream open];
}

// 接收到數(shù)據(jù)--一點點接收
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    // 將每次接收到的字節(jié)長度與已有的文件長度相加
    self.currentFileSize += data.length;
    // 當(dāng)前已下載的文件長度 * 1.0 / 文件的總長度 = 當(dāng)前的進(jìn)度
    float process = self.currentFileSize * 1.0 / self.expectedContentLength;
//    NSLog(@"接收數(shù)據(jù): %f", process);
    // 保存數(shù)據(jù)
    [self saveData:data];
    // 判斷進(jìn)度回調(diào)是否為空
    if (self.processBlock) {
        // 如果不為空則回到主線程
        dispatch_async(dispatch_get_main_queue(), ^{
            self.processBlock(process);
        });
    }
}

// 下載完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//    NSLog(@"下載完成");
    // 下載完成的時候阳谍,關(guān)閉流
    [self.outputStream close];
    
    // 如果下載回調(diào)不為空
    if (self.successBlock) {
        // 分發(fā)消息到主線程蛀柴,讓主線程來處理
        dispatch_async(dispatch_get_main_queue(), ^{
            self.successBlock(self.filePath);
        });
    }
}

// 下載出錯
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
//    NSLog(@"下載出錯");
    // 判斷流是否為空,下載出錯的時候矫夯,關(guān)閉流
    if (self.outputStream) {
        [self.outputStream close];
    }
    // 判斷異趁福回調(diào)是否為空
    if (self.errorBlock) {
        // 不為空回到主線程
        dispatch_async(dispatch_get_main_queue(), ^{
            self.errorBlock(error);
        });
    }
}

// NSOutputStream保存數(shù)據(jù)
// data 每次下載的二進(jìn)制文件
- (void)saveData:(NSData *)data {
    // 寫數(shù)據(jù)
    [self.outputStream write:data.bytes maxLength:data.length];
}

// NSFileHandle保存數(shù)據(jù)
// data 每次下載的二進(jìn)制數(shù)據(jù)
- (void)saveData1:(NSData *)data {
    // 保存文件的路徑
    NSString *filePath = @"/Users/mazaiting/Desktop/1.zip";
    // 根據(jù)路徑獲取NSFileHandle, 它不會自動創(chuàng)建文件,而是第0個字節(jié)添加數(shù)據(jù)
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
    // 如果fileHandle存在茧痒,則讓fileHandle的offset偏移量指向文件的末尾肮韧,進(jìn)行數(shù)據(jù)的添加
    // 如果fileHandle不存在,則創(chuàng)建文件
    if (fileHandle) {
        // fileHandle存在
        // 將文件指針指向文件的末尾
        [fileHandle seekToEndOfFile];
        // 寫數(shù)據(jù)
        [fileHandle writeData:data];
        // 關(guān)閉文件
        [fileHandle closeFile];
    } else {
        // fileHandle不存在
        [data writeToFile:filePath atomically:YES];
    }
}

@end

ViewController.m中的調(diào)用

#import "ViewController.h"
#import "MTDownloader.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    MTDownloader *downloader = [MTDownloader new];
    NSString *url = @"http://127.0.0.1/HBuilder.zip";
    [downloader download:url successBlock:^(NSString *path) {
        NSLog(@"下載成功");
    } processBlock:^(float process) {
        NSLog(@"下載進(jìn)度: %f", process);
    } errorBlock:^(NSError *error) {
        NSLog(@"下載出錯 %@", error);
    }];
}

@end

6. 進(jìn)度條顯示--- 自定義圓形進(jìn)度條

#import "ViewController.h"
#import "MTDownloader.h"
#import "MTProcessView.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet MTProcessView *processView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    MTDownloader *downloader = [MTDownloader new];
    NSString *url = @"http://127.0.0.1/HBuilder.zip";
    [downloader download:url successBlock:^(NSString *path) {
        NSLog(@"下載成功");
    } processBlock:^(float process) {
        self.processView.process = process;
        NSLog(@"下載進(jìn)度: %f", process);
    } errorBlock:^(NSError *error) {
        NSLog(@"下載出錯 %@", error);
    }];
}

@end

7. 暫停下載

在MTDownloader.h中添加暫停下載方法

#import <Foundation/Foundation.h>

@interface MTDownloader : NSObject
// ... 省略其他方法
// 暫停下載
- (void)pause;
@end

在MTDownloader.m中實現(xiàn)該方法旺订, 以下內(nèi)容為該類修改的方法弄企。

#import "MTDownloader.h"
// 分類
@interface MTDownloader () <NSURLConnectionDataDelegate>
// 下載時創(chuàng)建的NSURLConnection,可用于取消下載
@property (nonatomic, strong) NSURLConnection *urlConnection;
@end

@implementation MTDownloader
// 從指定位置處下載文件
- (void)downloadFile:(NSURL *)url {
    // 創(chuàng)建一個新的線程区拳,并添加到隊列中
    [[NSOperationQueue new] addOperationWithBlock:^{
        // 創(chuàng)建一個請求
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        // 請求頭
        // Range:bytes=x-y 從x個字節(jié)下載到y(tǒng)個字節(jié)
        // Range:bytes=x-  從x個字節(jié)下載到最后
        // Range:bytes=-y  從開始下載到y(tǒng)個字節(jié)
        [request setValue:[NSString stringWithFormat:@"bytes=%lld-", self.currentFileSize] forHTTPHeaderField:@"Range"];
        
        // 設(shè)置代理, 實現(xiàn)NSURLConnectionDataDelegate協(xié)議
        self.urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        // 開啟消息循環(huán)
        [[NSRunLoop currentRunLoop] run];
    }];
    
}
// 暫停下載
- (void)pause {
    // 取消下載
    [self.urlConnection cancel];
}

8. 下載類的封裝

MTDownloadManager.h


#import <Foundation/Foundation.h>

@interface MTDownloadManager : NSObject
// 單例
+ (instancetype)sharedManager;
// 下載方法
// netUrl 下載的網(wǎng)絡(luò)連接
// successBlock 成功回調(diào)block
// processBlock 進(jìn)度對調(diào)block
// errorBlock 異尘辛欤回調(diào)block
- (void)download:(NSString *)netUrl successBlock:(void(^)(NSString *path))successBlock processBlock:(void(^)(float process))processBlock errorBlock:(void(^)(NSError *error))errorBlock;

// 暫停下載
- (void)pause:(NSString *)netUrl;
@end

MTDownloadManager.m

#import "MTDownloadManager.h"
#import "MTDownloader.h"

@interface MTDownloadManager ()
// 下載操作緩存池
@property (nonatomic, strong) NSMutableDictionary *downloadCache;
@end

@implementation MTDownloadManager
// 單例
+ (instancetype)sharedManager {
    static id instance = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [self new];
    });
    return instance;
}

// 懶加載
- (NSMutableDictionary *)downloadCache {
    if (_downloadCache == nil) {
        _downloadCache = [NSMutableDictionary dictionaryWithCapacity:10];
    }
    return _downloadCache;
}
// 下載
- (void)download:(NSString *)netUrl successBlock:(void (^)(NSString *))successBlock processBlock:(void (^)(float))processBlock errorBlock:(void (^)(NSError *))errorBlock {
    // 判斷緩存池中是否有下載操作
    if (self.downloadCache[netUrl]) {
        NSLog(@"正在拼命下載中...");
        return;
    }
    // 新建下載
    MTDownloader *downloader = [MTDownloader download:netUrl successBlock:^(NSString *path) {
        // 移除下載操作
        [self.downloadCache removeObjectForKey:netUrl];
        if (successBlock) {
            successBlock(path);
        }
    } processBlock:processBlock errorBlock:^(NSError *error) {
        // 移除下載操作
        [self.downloadCache removeObjectForKey:netUrl];
        if (errorBlock) {
            errorBlock(error);
        }
    }];
    [[NSOperationQueue new] addOperation:downloader];
    
    // 添加到緩存池中
    [self.downloadCache setObject:downloader forKey:netUrl];
}
// 暫停
- (void)pause:(NSString *)netUrl {
    MTDownloader *downloader = self.downloadCache[netUrl];
    // 判斷是否為空,如果不為空樱调,則暫停约素;如果為空,則什么都不做
    if (downloader == nil) {
        NSLog(@"沒有下載操作笆凌,無法暫停");
        return;
    }
    
    [downloader pause];
    
    // 刪除緩存池中的下載操作
    [self.downloadCache removeObjectForKey:netUrl];
}

@end

MTDownloader.h


#import <Foundation/Foundation.h>

@interface MTDownloader : NSOperation
// 下載方法
// netUrl 下載的網(wǎng)絡(luò)連接
// successBlock 成功回調(diào)block
// processBlock 進(jìn)度對調(diào)block
// errorBlock 異呈チ裕回調(diào)block
+ (instancetype)download:(NSString *)netUrl successBlock:(void(^)(NSString *path))successBlock processBlock:(void(^)(float process))processBlock errorBlock:(void(^)(NSError *error))errorBlock;

// 暫停下載
- (void)pause;
@end

MTDownloader.m

// 斷點續(xù)傳思路
// 1. 首先發(fā)送一個head請求,獲取到服務(wù)器文件的大小和名稱
// 2. 判斷本地文件是否存在
//      1). 不存在乞而,從0開始下載
//      2). 存在
//          a. 本地文件大小 == 服務(wù)器文件大小  不需要下載
//          b. 本地文件大小 < 服務(wù)器文件大小   從當(dāng)前位置下載
//          c. 本地文件打下 > 服務(wù)器文件大小   刪除文件送悔,從0下載
#import "MTDownloader.h"
// 分類
@interface MTDownloader () <NSURLConnectionDataDelegate>
// 記錄文件的總長度,因為在獲取下載進(jìn)度的時候需要文件總長度,所以定義為屬性
@property (nonatomic, assign) long long expectedContentLength;
// 記錄當(dāng)前文件的長度
@property (nonatomic, assign) long long currentFileSize;
// NSOutputStream用來保存每次下載的二進(jìn)制數(shù)據(jù)
@property (nonatomic, strong) NSOutputStream *outputStream;
// 下載時創(chuàng)建的NSURLConnection爪模,可用于取消下載
@property (nonatomic, strong) NSURLConnection *urlConnection;
// 文件的存儲路徑
@property (nonatomic, strong) NSString *filePath;
// 下載成功后的回調(diào)
@property (nonatomic, copy) void (^successBlock)(NSString *path);
// 下載進(jìn)度回調(diào)
@property (nonatomic, copy) void (^processBlock)(float process);
// 下載異常的回調(diào)
@property (nonatomic, copy) void (^errorBlock)(NSError *error);
// 下載鏈接
@property (nonatomic, copy) NSString *urlString;
@end

@implementation MTDownloader
// 下載
+ (instancetype)download:(NSString *)netUrl successBlock:(void (^)(NSString *))successBlock processBlock:(void (^)(float))processBlock errorBlock:(void (^)(NSError *))errorBlock {
    MTDownloader *downloader = [MTDownloader new];
    downloader.successBlock = successBlock;
    downloader.processBlock = processBlock;
    downloader.errorBlock = errorBlock;
    downloader.urlString = netUrl;
    return downloader;
}

- (void)main {
    @autoreleasepool {
        // 創(chuàng)建url
        NSURL *url = [NSURL URLWithString:self.urlString];
        // 獲取服務(wù)器文件信息, 獲取當(dāng)前文件長度
        [self getServerFileInfo:url];
        // 獲取本地文件信息欠啤,并且和服務(wù)器做比較
        self.currentFileSize = [self getLocalFileInfo];
        // 判斷當(dāng)前文件大小是否是-1,如果是-1屋灌,說明文件已經(jīng)下載完成
        if (self.currentFileSize == -1) {
            //        NSLog(@"文件已下載完成洁段,請不要重復(fù)下載!!!");
            // 如果下載回調(diào)不為空
            if (self.successBlock) {
                // 分發(fā)消息到主線程,讓主線程來處理
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.successBlock(self.filePath);
                });
            }
            return;
        }
        
        // 從指定位置處下載文件
        [self downloadFile: url];
    }
}

// 從指定位置處下載文件
- (void)downloadFile:(NSURL *)url {
    // 創(chuàng)建一個新的線程共郭,并添加到隊列中
    [[NSOperationQueue new] addOperationWithBlock:^{
        // 創(chuàng)建一個請求
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        // 請求頭
        // Range:bytes=x-y 從x個字節(jié)下載到y(tǒng)個字節(jié)
        // Range:bytes=x-  從x個字節(jié)下載到最后
        // Range:bytes=-y  從開始下載到y(tǒng)個字節(jié)
        [request setValue:[NSString stringWithFormat:@"bytes=%lld-", self.currentFileSize] forHTTPHeaderField:@"Range"];
        
        // 設(shè)置代理, 實現(xiàn)NSURLConnectionDataDelegate協(xié)議
        self.urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        // 開啟消息循環(huán)
        [[NSRunLoop currentRunLoop] run];
    }];
    
}

// 檢測本地文件大小
- (long long)getLocalFileInfo {
    // 文件的長度祠丝, 如果文件不存在,則為0
    long long fileSize = 0;
    // 獲取文件管理者對象
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // 判斷文件是否存在
    if ([fileManager fileExistsAtPath:self.filePath]) {
        // 文件存在
        // 獲取本地文件的大小
        NSDictionary *attrs = [fileManager attributesOfItemAtPath:self.filePath error:NULL];
        // 賦值
        fileSize = attrs.fileSize;
        // 文件大小等于服務(wù)器文件長度
        if (fileSize == self.expectedContentLength) {
            fileSize = -1;
        }
        // 文件大小大于服務(wù)器文件長度
        if (fileSize > self.expectedContentLength) {
            fileSize = 0;
            // 刪除文件
            [fileManager removeItemAtPath:self.filePath error:NULL];
        }
    }
    return fileSize;
}

// 獲取服務(wù)器文件信息
// 文件的url
- (void)getServerFileInfo:(NSURL *)url {
    // 創(chuàng)建一個請求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    // 設(shè)置請求方法
    request.HTTPMethod = @"head";
    // 同步請求響應(yīng)體
    NSURLResponse *response = nil;
    // 發(fā)送一個同步請求
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];
    
    // 設(shè)置文件的總長度落塑,注意:在下載時- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;方法中為當(dāng)前文件總長度賦值的語句要刪除
    self.expectedContentLength = response.expectedContentLength;
    // 設(shè)置文件的存儲路徑, 存儲在臨時目錄下纽疟,拼接文件名
    self.filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:response.suggestedFilename];
//    NSLog(@"%@", self.filePath);
}

// 暫停下載
- (void)pause {
    // 取消下載
    [self.urlConnection cancel];
    // 取消自定義操作
    [self cancel];
}

#pragma mark- 代理方法
// 接收到響應(yīng)頭
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
//    NSLog(@"接收到響應(yīng) %@", response);
    // 接收到響應(yīng)頭的時候初始化outputStream
    self.outputStream = [NSOutputStream outputStreamToFileAtPath:self.filePath append:YES];
    // 打開流
    [self.outputStream open];
}

// 接收到數(shù)據(jù)--一點點接收
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    // 將每次接收到的字節(jié)長度與已有的文件長度相加
    self.currentFileSize += data.length;
    // 當(dāng)前已下載的文件長度 * 1.0 / 文件的總長度 = 當(dāng)前的進(jìn)度
    float process = self.currentFileSize * 1.0 / self.expectedContentLength;
//    NSLog(@"接收數(shù)據(jù): %f", process);
    // 保存數(shù)據(jù)
    [self saveData:data];
    // 判斷進(jìn)度回調(diào)是否為空
    if (self.processBlock) {
        // 如果不為空則回到主線程
        dispatch_async(dispatch_get_main_queue(), ^{
            self.processBlock(process);
        });
    }
}

// 下載完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//    NSLog(@"下載完成");
    // 下載完成的時候,關(guān)閉流
    [self.outputStream close];
    
    // 如果下載回調(diào)不為空
    if (self.successBlock) {
        // 分發(fā)消息到主線程憾赁,讓主線程來處理
        dispatch_async(dispatch_get_main_queue(), ^{
            self.successBlock(self.filePath);
        });
    }
}

// 下載出錯
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
//    NSLog(@"下載出錯");
    // 判斷流是否為空污朽,下載出錯的時候,關(guān)閉流
    if (self.outputStream) {
        [self.outputStream close];
    }
    // 判斷異沉迹回調(diào)是否為空
    if (self.errorBlock) {
        // 不為空回到主線程
        dispatch_async(dispatch_get_main_queue(), ^{
            self.errorBlock(error);
        });
    }
}

// NSOutputStream保存數(shù)據(jù)
// data 每次下載的二進(jìn)制文件
- (void)saveData:(NSData *)data {
    // 寫數(shù)據(jù)
    [self.outputStream write:data.bytes maxLength:data.length];
}

// NSFileHandle保存數(shù)據(jù)
// data 每次下載的二進(jìn)制數(shù)據(jù)
- (void)saveData1:(NSData *)data {
    // 保存文件的路徑
    NSString *filePath = @"/Users/mazaiting/Desktop/1.zip";
    // 根據(jù)路徑獲取NSFileHandle, 它不會自動創(chuàng)建文件蟆肆,而是第0個字節(jié)添加數(shù)據(jù)
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
    // 如果fileHandle存在矾睦,則讓fileHandle的offset偏移量指向文件的末尾,進(jìn)行數(shù)據(jù)的添加
    // 如果fileHandle不存在炎功,則創(chuàng)建文件
    if (fileHandle) {
        // fileHandle存在
        // 將文件指針指向文件的末尾
        [fileHandle seekToEndOfFile];
        // 寫數(shù)據(jù)
        [fileHandle writeData:data];
        // 關(guān)閉文件
        [fileHandle closeFile];
    } else {
        // fileHandle不存在
        [data writeToFile:filePath atomically:YES];
    }
}

@end

效果圖.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末枚冗,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子蛇损,更是在濱河造成了極大的恐慌赁温,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,525評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件淤齐,死亡現(xiàn)場離奇詭異股囊,居然都是意外死亡,警方通過查閱死者的電腦和手機更啄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評論 3 395
  • 文/潘曉璐 我一進(jìn)店門稚疹,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人祭务,你說我怎么就攤上這事内狗。” “怎么了义锥?”我有些...
    開封第一講書人閱讀 164,862評論 0 354
  • 文/不壞的土叔 我叫張陵柳沙,是天一觀的道長。 經(jīng)常有香客問我缨该,道長偎行,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,728評論 1 294
  • 正文 為了忘掉前任贰拿,我火速辦了婚禮,結(jié)果婚禮上熄云,老公的妹妹穿的比我還像新娘膨更。我一直安慰自己,他們只是感情好缴允,可當(dāng)我...
    茶點故事閱讀 67,743評論 6 392
  • 文/花漫 我一把揭開白布荚守。 她就那樣靜靜地躺著,像睡著了一般练般。 火紅的嫁衣襯著肌膚如雪矗漾。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,590評論 1 305
  • 那天薄料,我揣著相機與錄音敞贡,去河邊找鬼。 笑死摄职,一個胖子當(dāng)著我的面吹牛誊役,可吹牛的內(nèi)容都是我干的获列。 我是一名探鬼主播,決...
    沈念sama閱讀 40,330評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼蛔垢,長吁一口氣:“原來是場噩夢啊……” “哼击孩!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起鹏漆,我...
    開封第一講書人閱讀 39,244評論 0 276
  • 序言:老撾萬榮一對情侶失蹤巩梢,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后艺玲,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體括蝠,經(jīng)...
    沈念sama閱讀 45,693評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,885評論 3 336
  • 正文 我和宋清朗相戀三年板驳,在試婚紗的時候發(fā)現(xiàn)自己被綠了又跛。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,001評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡若治,死狀恐怖慨蓝,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情端幼,我是刑警寧澤礼烈,帶...
    沈念sama閱讀 35,723評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站婆跑,受9級特大地震影響此熬,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜滑进,卻給世界環(huán)境...
    茶點故事閱讀 41,343評論 3 330
  • 文/蒙蒙 一犀忱、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧扶关,春花似錦阴汇、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至铜异,卻和暖如春哥倔,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背揍庄。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評論 1 270
  • 我被黑心中介騙來泰國打工咆蒿, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 48,191評論 3 370
  • 正文 我出身青樓蜡秽,卻偏偏與公主長得像府阀,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子芽突,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,955評論 2 355

推薦閱讀更多精彩內(nèi)容