使用AFNetWorking下載文件時损谦,需要包含AFNetWorking框架(使用的是2.3.0版本)
當只需單個文件下載時蛙吏,可以使用單例模式
.h 文件
#import <AFNetworking.h>
@interface DownLoadOrUpLoadFileManager : NSObject
@property (nonatomic, strong) AFHTTPRequestSerializer *serializer;
@property (nonatomic, strong) AFHTTPRequestOperation *downLoadOperation;
+ (instancetype)getInstance;
#pragma mark -下載文件
/**
* 根據(jù)url判斷是否已經(jīng)保存到本地了
*
* @param created 文件的創(chuàng)建時間 通過和fileName拼接成完整的文件路徑
*
* @param fileName 文件的名字
*
* @return YES:本地已經(jīng)存在座哩,NO:本地不存在
*/
- (BOOL)isSavedFileToLocalWithCreated:(UInt32)created fileName:(NSString *)fileName;
/**
* 根據(jù)文件的創(chuàng)建時間 設置保存到本地的路徑
*
* @param created 創(chuàng)建時間
* @param fileName 名字
*
* @return <#return value description#>
*/
-(NSString *)setPathOfDocumentsByFileCreated:(UInt32)created fileName:(NSString *)fileName;
/**
* 根據(jù)文件類型兴革、名字盐欺、創(chuàng)建時間獲得本地文件的路徑螟炫,當文件不存在時波附,返回nil
*
* @param fileType 文件類型
* @param fileName 文件名字
* @param created 文件在服務器創(chuàng)建的時間
*
* @return
*/
- (NSURL *)getLocalFilePathWithFileType:(HomeWorkFileType)fileType fileName:(NSString *)fileName fileCreated:(UInt32)created;
/**
* @brief 下載文件
*
* @param paramDic 額外的參數(shù)
* @param requestURL 下載的url
* @param fileType 文件類型
* @param fileName 文件名字
* @param created 文件服務器創(chuàng)建時間
* @param success
* @param failure
* @param progress
*/
- (void)downloadFileWithOption:(NSDictionary *)paramDic
withFileUrl:(NSString*)requestURL
fileType:(HomeWorkFileType)fileType
fileName:(NSString *)fileName
fileCreated:(UInt32)created
downloadSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
downloadFailure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
progress:(void (^)(float progress, long long downloadLength, long long totleLength))progress;
/**
* 取消下載,并刪除本地已經(jīng)下載了的部分
*
* @param created 文件在服務器創(chuàng)建的時間
* @param fileName 文件的名字
*/
- (void)cancleDownLoadFileWithServiceCreated:(UInt32)created fileName:(NSString *)fileName;
/**
* 正在下載中
*
* @return
*/
- (BOOL)isDownLoadExecuting;
/**
* 下載暫停
*/
- (void)downLoadPause;
/**
* 下載繼續(xù)
*/
- (void)downLoadResume;
.m 文件
@implementation DownLoadOrUpLoadFileManager
+ (instancetype)getInstance
{
static DownLoadOrUpLoadFileManager *manager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[DownLoadOrUpLoadFileManager alloc]init];
manager.serializer = [AFHTTPRequestSerializer serializer];
});
return manager;
}
/**
* 根據(jù)url判斷是否已經(jīng)保存到本地了
*
* @param url 文件的url
*
* @return YES:本地已經(jīng)存在昼钻,NO:本地不存在
*/
- (BOOL)isSavedFileToLocalWithCreated:(UInt32)created fileName:(NSString *)fileName
{
// 判斷是否已經(jīng)離線下載了
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%d/%@", LOCAL_SAVE_PATH, created, fileName]];
NSFileManager *filemanager = [NSFileManager defaultManager];
if ([filemanager fileExistsAtPath:path]) {
return YES;
}
return NO;
}
/**
* 根據(jù)文件的創(chuàng)建時間 設置保存到本地的路徑
*
* @param created 創(chuàng)建時間
* @param fileName 名字
*
* @return <#return value description#>
*/
-(NSString *)setPathOfDocumentsByFileCreated:(UInt32)created fileName:(NSString *)fileName
{
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%d", LOCAL_SAVE_PATH, created]];
NSFileManager *filemanager = [NSFileManager defaultManager];
if (![filemanager fileExistsAtPath:path]) {
[filemanager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
}
return path;
}
/**
* 根據(jù)文件類型掸屡、名字、創(chuàng)建時間獲得本地文件的路徑然评,當文件不存在時仅财,返回nil
*
* @param fileType 文件類型
* @param fileName 文件名字
* @param created 文件在服務器創(chuàng)建的時間
*
* @return
*/
- (NSURL *)getLocalFilePathWithFileType:(HomeWorkFileType)fileType fileName:(NSString *)fileName fileCreated:(UInt32)created
{
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%d", LOCAL_SAVE_PATH, created]];
NSFileManager *filemanager = [NSFileManager defaultManager];
NSArray *fileList = [filemanager subpathsOfDirectoryAtPath:path error:nil];
if ([fileList containsObject:fileName]) {
NSString *fileUrltemp = [NSString stringWithFormat:@"%@/%@", path, fileName];
NSURL *url = [NSURL fileURLWithPath:fileUrltemp];
return url;
}
return nil;
}
/**
* @brief 下載文件
*
* @param paramDic 額外的參數(shù)
* @param requestURL 下載的url
* @param fileType 文件類型
* @param fileName 文件名字
* @param created 文件服務器創(chuàng)建時間
* @param success
* @param failure
* @param progress
*/
- (void)downloadFileWithOption:(NSDictionary *)paramDic
withFileUrl:(NSString*)requestURL
fileType:(HomeWorkFileType)fileType
fileName:(NSString *)fileName
fileCreated:(UInt32)created
downloadSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
downloadFailure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
progress:(void (^)(float progress, long long downloadLength, long long totleLength))progress;
{
//沙盒路徑 //NSString *savedPath = [NSHomeDirectory() stringByAppendingString:@"/Documents/xxx.zip"];
NSMutableURLRequest *request =[_serializer requestWithMethod:@"POST" URLString:requestURL parameters:paramDic error:nil];
_downLoadOperation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
NSString *localSavePath = [NSString stringWithFormat:@"%@/%@", [self setPathOfDocumentsByFileCreated:created fileName:fileName], fileName];
[_downLoadOperation setOutputStream:[NSOutputStream outputStreamToFileAtPath:localSavePath append:NO]];
[_downLoadOperation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
float p = (float)totalBytesRead / totalBytesExpectedToRead;
if (progress) {
progress(p, totalBytesRead, totalBytesExpectedToRead);
}
}];
[_downLoadOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
if (success) {
success(operation,responseObject);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failure) {
failure(operation,error);
}
}];
[_downLoadOperation start];
}
/**
* 取消下載,并刪除本地已經(jīng)下載了的部分
*
* @param created 文件在服務器創(chuàng)建的時間
* @param fileName 文件的名字
*/
- (void)cancleDownLoadFileWithServiceCreated:(UInt32)created fileName:(NSString *)fileName;
{
[_downLoadOperation cancel];
// 刪除本地文件
NSString *localSavePath = [NSString stringWithFormat:@"%@/%@", [self setPathOfDocumentsByFileCreated:created fileName:fileName], fileName];
NSFileManager *filemanager = [NSFileManager defaultManager];
if ([filemanager fileExistsAtPath:localSavePath]) {
[filemanager removeItemAtPath:localSavePath error:nil];
}
}
// 正在下載中
- (BOOL)isDownLoadExecuting
{
return [_downLoadOperation isExecuting];
}
// 下載暫停
- (void)downLoadPause
{
[_downLoadOperation pause];
}
// 下載繼續(xù)
- (void)downLoadResume
{
[_downLoadOperation resume];
}
在下載之前可以先判斷一下文件是否已經(jīng)保存到本地
· - (BOOL)isSavedFileToLocalWithCreated:(UInt32)created fileName:(NSString *)fileName碗淌;