多线程下文件的代码
2012-04-24 10:11 java环境变量 阅读(205) 评论(0) 编辑 收藏 举报.h
-
NSString*FDownLoadDefaultFile; //下载异常状态的默认文件
-
NSString*FDownLoadLocalFilePath; //下载到本地的保存路径
-
NSIntegerFDownLoadFileCount; //当前下载的文件总数
-
NSInteger FDownLoadID; //下载序列ID
-
NSMutableArray*FDownLoadFileArr; //下载到本地的文件
-
NSMutableArray*FDownLoadNetworkFilePathArr; //需要下载到本地的网络文件路径
-
NSCondition*FDownloadLockCondition; //线程锁
-
-
//下载文件
-
//多线程下载文件
-
-(void)DownloadFileByThread : (NSArray *)AFileArr //需要下载的文件集合数组
-
ADownloadDirPath : (NSString *)ADownloadFilePath //需要下载到本地的目录路径,结尾为“/”
-
ADownloadDefaultFileURL : (NSString *)ADownloadDeaultFileURL; //如果需要下载的文件不存在,则需要下载的默认文件
-
//下载主线程
-
-(void)DownloadMainThread : (NSArray *)ADataArr; //传递到线程中的数据集合,
-
//位置0=本地下载目录
-
//位置1=下载文件地址
-
//位置2=默认下载文件
- //位置3=文件所属的位置索引号
-
-(void)SetDownloadFileID : (NSInteger)AID {
-
FDownLoadID= AID;
-
}
-
-
-(void)DownloadFileByThread : (NSArray *)AFileArr
-
ADownloadDirPath : (NSString *)ADownloadFilePath
-
ADownloadDefaultFileURL : (NSString *)ADownloadDeaultFileURL {
-
NSInteger AFileCount = [AFileArr count];
-
NSString *ALocalFile = ADownloadFilePath;
-
NSString *AFileName;
-
NSString *AFileURL;
-
NSString *ADefaultFile;
-
NSMutableArray*ADataArr;
-
-
[[NSURLCachesharedURLCache] removeAllCachedResponses];
-
FDownLoadNetworkFilePathArr= [[NSMutableArrayalloc] initWithArray:AFileArr];
-
FDownLoadFileArr = [[NSMutableArray alloc] initWithCapacity:AFileCount];
-
FDownLoadFileCount= 0;
-
FDownloadLockCondition= [[NSConditionalloc] init];
-
-
for (int i = 0; i < AFileCount; i++) {
-
[FDownLoadFileArraddObject:@""];
-
}
-
-
for (int i = 0; i < AFileCount; i++) {
-
AFileURL = [[NSString alloc] initWithString: [AFileArr objectAtIndex : i]];
-
AFileName = [ALocalFile stringByAppendingString:[self GetFileName:AFileURL]];
-
-
ADataArr = [[NSMutableArray alloc] initWithCapacity:3];
-
[ADataArr addObject:ALocalFile];
-
if([[NSFileManagerdefaultManager] fileExistsAtPath:AFileName] == NO) {
-
//NSLog(@"%@",AFileURL);
-
[ADataArr addObject:AFileURL];
-
[ADataArr addObject:ADownloadDeaultFileURL];
-
[ADataArr addObject:[[NSStringalloc] initWithFormat:@"%d",i]];
-
-
[NSThreaddetachNewThreadSelector:@selector(DownloadMainThread:) toTarget:selfwithObject:ADataArr];
-
[ADataArr release];
-
}
-
else {
-
//NSLog(@"%@",AFileName);
-
if (AFileURL == @"") {
-
ADefaultFile = [ALocalFile stringByAppendingString:[self GetFileName:ADownloadDeaultFileURL]];
-
if ([[NSFileManager defaultManager] fileExistsAtPath:ADefaultFile] == NO) {
-
[ADataArr addObject:ADownloadDeaultFileURL];
-
[ADataArr addObject:ADownloadDeaultFileURL];
-
[ADataArr addObject:[[NSStringalloc] initWithFormat:@"%d",i]];
-
[NSThreaddetachNewThreadSelector:@selector(DownloadMainThread:) toTarget:selfwithObject:ADataArr];
-
[ADataArr release];
-
}
-
else {
-
FDownLoadFileCount++;
-
[FDownLoadFileArrreplaceObjectAtIndex:i withObject:ADefaultFile];
-
//[FDownLoadFileArr addObject:ADefaultFile];
-
}
-
}
-
else {
-
FDownLoadFileCount++;
-
[FDownLoadFileArrreplaceObjectAtIndex:i withObject:AFileName];
-
//[FDownLoadFileArr setValue:AFileName forKey:[[NSString alloc] initWithFormat:@"%d",i]];
-
//[FDownLoadFileArr addObject:AFileName];
-
}
-
-
}
-
-
}
-
if(FDownLoadFileCount== [FDownLoadNetworkFilePathArrcount]) { //完成全部下载
-
[self.delegate DownLoadFileDidFinish:FDownLoadFileArr AID : FDownLoadID];
-
[FDownLoadNetworkFilePathArrrelease];
-
[FDownLoadFileArrrelease];
-
[FDownloadLockConditionrelease];
-
-
}
-
}
-
-
-(void)DownloadMainThread : (NSArray *)ADataArr {
-
NSString *ALocalFile = [ADataArr objectAtIndex:0];
-
//NSLog(@"%@",[ADataArr objectAtIndex:1]);
-
NSURL*AURL = [[NSURLalloc] initWithString:[ADataArr objectAtIndex:1]];
-
NSData *AFileData = [[NSData alloc] initWithContentsOfURL:AURL];
-
if (AFileData != nil) {
-
ALocalFile = [ALocalFile stringByAppendingString:[self GetFileName:[ADataArr objectAtIndex:1]]];
-
//NSLog(@"%@",ALocalFile);
-
[AFileData writeToFile:ALocalFile atomically:YES];
-
}
-
else{ //不存在指定路径的文件,使用默认文件
-
NSString *ADefaultFile = [ALocalFile stringByAppendingString:[self GetFileName:[ADataArr objectAtIndex:2]]];
-
if ([[NSFileManager defaultManager] fileExistsAtPath:ADefaultFile] == NO) {
-
[AURL release];
-
[AFileData release];
-
AURL = [[NSURLalloc] initWithString:[ADataArr objectAtIndex:2]];
-
AFileData = [[NSData alloc] initWithContentsOfURL:AURL];
-
if (AFileData != nil) {
-
ALocalFile = [ALocalFile stringByAppendingString:[self GetFileName:[ADataArr objectAtIndex:1]]];
-
[AFileData writeToFile:ALocalFile atomically:YES];
-
}
-
else {
-
ALocalFile = [DocumentPathstringByAppendingString:@"default.png"];
-
}
-
}
-
}
-
//NSLog(@"ADataArr = %@",[ADataArr objectAtIndex:3]);
-
[FDownloadLockConditionlock]; //上锁
-
FDownLoadFileCount++;
-
[FDownLoadFileArrreplaceObjectAtIndex:[[ADataArr objectAtIndex:3] intValue] withObject:ALocalFile];
-
[FDownloadLockConditionunlock]; //解锁
-
-
[AURL release];
-
[ALocalFile release];
-
[AFileData release];
-
-
if(FDownLoadFileCount== [FDownLoadNetworkFilePathArrcount]) { //完成全部下载
-
[self.delegate DownLoadFileDidFinish:FDownLoadFileArr AID : FDownLoadID];
-
[FDownLoadNetworkFilePathArrrelease];
-
[FDownLoadFileArrrelease];
-
[FDownloadLockConditionrelease];
-
}
- }