代码改变世界

多线程下文件的代码

2012-04-24 10:11  java环境变量  阅读(205)  评论(0编辑  收藏  举报

.h
  1. NSString*FDownLoadDefaultFile;   //下载异常状态的默认文件
  2. NSString*FDownLoadLocalFilePath; //下载到本地的保存路径
  3. NSIntegerFDownLoadFileCount;     //当前下载的文件总数
  4. NSInteger FDownLoadID;            //下载序列ID
  5. NSMutableArray*FDownLoadFileArr;        //下载到本地的文件
  6. NSMutableArray*FDownLoadNetworkFilePathArr;    //需要下载到本地的网络文件路径
  7. NSCondition*FDownloadLockCondition;  //线程锁

  8. //下载文件
  9. //多线程下载文件
  10. -(void)DownloadFileByThread : (NSArray *)AFileArr                 //需要下载的文件集合数组
  11.     ADownloadDirPath : (NSString *)ADownloadFilePath       //需要下载到本地的目录路径,结尾为“/”
  12. ADownloadDefaultFileURL : (NSString *)ADownloadDeaultFileURL; //如果需要下载的文件不存在,则需要下载的默认文件
  13. //下载主线程
  14. -(void)DownloadMainThread : (NSArray *)ADataArr;      //传递到线程中的数据集合,
  15.       //位置0=本地下载目录
  16. //位置1=下载文件地址
  17.       //位置2=默认下载文件
  18. //位置3=文件所属的位置索引号
复制代码
.m
  1. -(void)SetDownloadFileID : (NSInteger)AID {
  2. FDownLoadID= AID;
  3. }

  4. -(void)DownloadFileByThread : (NSArray *)AFileArr
  5.     ADownloadDirPath : (NSString *)ADownloadFilePath 
  6. ADownloadDefaultFileURL : (NSString *)ADownloadDeaultFileURL {
  7. NSInteger AFileCount = [AFileArr count];
  8. NSString *ALocalFile = ADownloadFilePath;
  9. NSString *AFileName;
  10. NSString *AFileURL;
  11. NSString *ADefaultFile;
  12. NSMutableArray*ADataArr;

  13. [[NSURLCachesharedURLCache] removeAllCachedResponses];
  14. FDownLoadNetworkFilePathArr= [[NSMutableArrayalloc] initWithArray:AFileArr];
  15. FDownLoadFileArr = [[NSMutableArray alloc] initWithCapacity:AFileCount];
  16. FDownLoadFileCount= 0;
  17. FDownloadLockCondition= [[NSConditionalloc] init];

  18. for (int i = 0; i < AFileCount; i++) {
  19. [FDownLoadFileArraddObject:@""];
  20. }

  21. for (int i = 0; i < AFileCount; i++) {
  22. AFileURL = [[NSString alloc] initWithString: [AFileArr objectAtIndex : i]];
  23. AFileName = [ALocalFile stringByAppendingString:[self GetFileName:AFileURL]];

  24. ADataArr = [[NSMutableArray alloc] initWithCapacity:3];
  25. [ADataArr addObject:ALocalFile];
  26. if([[NSFileManagerdefaultManager] fileExistsAtPath:AFileName] == NO) {
  27. //NSLog(@"%@",AFileURL);
  28. [ADataArr addObject:AFileURL];
  29. [ADataArr addObject:ADownloadDeaultFileURL];
  30. [ADataArr addObject:[[NSStringalloc] initWithFormat:@"%d",i]];

  31. [NSThreaddetachNewThreadSelector:@selector(DownloadMainThread:) toTarget:selfwithObject:ADataArr];
  32. [ADataArr release];
  33. }
  34. else {
  35. //NSLog(@"%@",AFileName);
  36. if (AFileURL == @"") {
  37. ADefaultFile = [ALocalFile stringByAppendingString:[self GetFileName:ADownloadDeaultFileURL]];
  38. if ([[NSFileManager defaultManager] fileExistsAtPath:ADefaultFile] == NO) {
  39. [ADataArr addObject:ADownloadDeaultFileURL];
  40. [ADataArr addObject:ADownloadDeaultFileURL];
  41. [ADataArr addObject:[[NSStringalloc] initWithFormat:@"%d",i]];
  42. [NSThreaddetachNewThreadSelector:@selector(DownloadMainThread:) toTarget:selfwithObject:ADataArr];
  43. [ADataArr release];
  44. }
  45. else {
  46. FDownLoadFileCount++;
  47. [FDownLoadFileArrreplaceObjectAtIndex:i withObject:ADefaultFile];
  48. //[FDownLoadFileArr addObject:ADefaultFile];
  49. }
  50. }
  51. else {
  52. FDownLoadFileCount++;
  53. [FDownLoadFileArrreplaceObjectAtIndex:i withObject:AFileName];
  54. //[FDownLoadFileArr setValue:AFileName forKey:[[NSString alloc] initWithFormat:@"%d",i]];
  55. //[FDownLoadFileArr addObject:AFileName];
  56. }

  57. }

  58. }
  59. if(FDownLoadFileCount== [FDownLoadNetworkFilePathArrcount]) {   //完成全部下载
  60. [self.delegate DownLoadFileDidFinish:FDownLoadFileArr AID : FDownLoadID];
  61. [FDownLoadNetworkFilePathArrrelease];
  62. [FDownLoadFileArrrelease];
  63. [FDownloadLockConditionrelease];

  64. }
  65. }

  66. -(void)DownloadMainThread : (NSArray *)ADataArr {
  67. NSString *ALocalFile = [ADataArr objectAtIndex:0];
  68. //NSLog(@"%@",[ADataArr objectAtIndex:1]);
  69. NSURL*AURL = [[NSURLalloc] initWithString:[ADataArr objectAtIndex:1]];
  70. NSData *AFileData = [[NSData alloc] initWithContentsOfURL:AURL];
  71. if (AFileData != nil) {
  72. ALocalFile = [ALocalFile stringByAppendingString:[self GetFileName:[ADataArr objectAtIndex:1]]];
  73. //NSLog(@"%@",ALocalFile);
  74. [AFileData writeToFile:ALocalFile atomically:YES];
  75. }
  76. else{    //不存在指定路径的文件,使用默认文件
  77. NSString *ADefaultFile = [ALocalFile stringByAppendingString:[self GetFileName:[ADataArr objectAtIndex:2]]];
  78. if ([[NSFileManager defaultManager] fileExistsAtPath:ADefaultFile] == NO) {
  79. [AURL release];
  80. [AFileData release];
  81. AURL = [[NSURLalloc] initWithString:[ADataArr objectAtIndex:2]];
  82. AFileData = [[NSData alloc] initWithContentsOfURL:AURL];
  83. if (AFileData != nil) {
  84. ALocalFile = [ALocalFile stringByAppendingString:[self GetFileName:[ADataArr objectAtIndex:1]]];
  85. [AFileData writeToFile:ALocalFile atomically:YES];
  86. }
  87. else {
  88. ALocalFile = [DocumentPathstringByAppendingString:@"default.png"];
  89. }
  90. }
  91. }
  92. //NSLog(@"ADataArr = %@",[ADataArr objectAtIndex:3]);
  93. [FDownloadLockConditionlock];    //上锁
  94. FDownLoadFileCount++;
  95. [FDownLoadFileArrreplaceObjectAtIndex:[[ADataArr objectAtIndex:3] intValue] withObject:ALocalFile];
  96. [FDownloadLockConditionunlock];  //解锁

  97. [AURL release];
  98. [ALocalFile release];
  99. [AFileData release];

  100. if(FDownLoadFileCount== [FDownLoadNetworkFilePathArrcount]) {   //完成全部下载
  101. [self.delegate DownLoadFileDidFinish:FDownLoadFileArr AID : FDownLoadID];
  102. [FDownLoadNetworkFilePathArrrelease];
  103. [FDownLoadFileArrrelease];
  104. [FDownloadLockConditionrelease];
  105. }
  106. }
复制代码