|
@@ -8,11 +8,73 @@
|
|
|
|
|
|
|
|
#import "RANetworkTaskDelegate.h"
|
|
#import "RANetworkTaskDelegate.h"
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+@interface JLFileStream : NSObject
|
|
|
|
|
+
|
|
|
|
|
+@property (nonatomic,copy) NSString *filePath;
|
|
|
|
|
+@property (nonatomic,strong) NSInputStream *inputStream;
|
|
|
|
|
+@property (nonatomic,strong) NSOutputStream *outputStream;
|
|
|
|
|
+
|
|
|
|
|
+@end
|
|
|
|
|
+
|
|
|
|
|
+@implementation JLFileStream
|
|
|
|
|
+
|
|
|
|
|
+#pragma mark - life
|
|
|
|
|
+
|
|
|
|
|
++ (instancetype)jl_fileStreamOfPath:(NSString *)path {
|
|
|
|
|
+
|
|
|
|
|
+ JLFileStream *stream = [[JLFileStream alloc] init];
|
|
|
|
|
+ stream.filePath = path;
|
|
|
|
|
+
|
|
|
|
|
+ return stream;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+- (void)setFilePath:(NSString *)filePath {
|
|
|
|
|
+ _filePath = filePath;
|
|
|
|
|
+
|
|
|
|
|
+ if (!filePath) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (_outputStream) {
|
|
|
|
|
+ [_outputStream close];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ _outputStream = [NSOutputStream outputStreamToFileAtPath:self.filePath append:YES];
|
|
|
|
|
+ [_outputStream open];
|
|
|
|
|
+
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+#pragma mark - out put stream
|
|
|
|
|
+
|
|
|
|
|
+- (void)jl_writeData:(NSData *)data {
|
|
|
|
|
+
|
|
|
|
|
+ [self writeData:data toPath:self.filePath];
|
|
|
|
|
+
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+- (void)writeData:(NSData *)data toPath:(NSString *)path {
|
|
|
|
|
+
|
|
|
|
|
+ [self.outputStream write:data.bytes maxLength:data.length];
|
|
|
|
|
+
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+- (void)jl_closeWriter {
|
|
|
|
|
+
|
|
|
|
|
+ [self.outputStream close];
|
|
|
|
|
+
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+@end
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
@interface RANetworkTaskDelegate ()
|
|
@interface RANetworkTaskDelegate ()
|
|
|
|
|
|
|
|
@property (nonatomic,strong) NSMutableData *recvData;
|
|
@property (nonatomic,strong) NSMutableData *recvData;
|
|
|
//@property (nonatomic,strong) NSMutableDictionary *result;
|
|
//@property (nonatomic,strong) NSMutableDictionary *result;
|
|
|
|
|
|
|
|
|
|
+@property (nonatomic,strong) JLFileStream *fileStream;
|
|
|
|
|
+
|
|
|
@end
|
|
@end
|
|
|
|
|
|
|
|
@implementation RANetworkTaskDelegate
|
|
@implementation RANetworkTaskDelegate
|
|
@@ -60,6 +122,10 @@
|
|
|
//
|
|
//
|
|
|
//
|
|
//
|
|
|
|
|
|
|
|
|
|
+ if (self.isDownloadTask) {
|
|
|
|
|
+ [self.fileStream jl_closeWriter];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
|
|
|
printf("net work complete\n");
|
|
printf("net work complete\n");
|
|
|
if (self.r) {
|
|
if (self.r) {
|
|
@@ -102,6 +168,36 @@
|
|
|
|
|
|
|
|
NSLog(@"Delegate recv response: %@",response);
|
|
NSLog(@"Delegate recv response: %@",response);
|
|
|
|
|
|
|
|
|
|
+ if (self.isDownloadTask) {
|
|
|
|
|
+
|
|
|
|
|
+ // 文件保存地址
|
|
|
|
|
+ if (!self.fileCachePath) {
|
|
|
|
|
+ self.fileCachePath = NSTemporaryDirectory();
|
|
|
|
|
+ }
|
|
|
|
|
+ NSString *desPath = self.fileCachePath;
|
|
|
|
|
+ NSError *err;
|
|
|
|
|
+ BOOL isDir = NO;
|
|
|
|
|
+ [[NSFileManager defaultManager] fileExistsAtPath:desPath isDirectory:&isDir];
|
|
|
|
|
+ if (isDir) {
|
|
|
|
|
+
|
|
|
|
|
+ desPath = [self.fileCachePath stringByAppendingPathComponent:response.suggestedFilename];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 如果文件存在
|
|
|
|
|
+ if ([[NSFileManager defaultManager] fileExistsAtPath:desPath]) {
|
|
|
|
|
+
|
|
|
|
|
+ [[NSFileManager defaultManager] removeItemAtPath:desPath error:&err];
|
|
|
|
|
+ if (err) {
|
|
|
|
|
+ completionHandler(NSURLSessionResponseCancel);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ self.fileCachePath = desPath;
|
|
|
|
|
+
|
|
|
|
|
+ self.fileStream = [JLFileStream jl_fileStreamOfPath:self.fileCachePath];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// self.result.response = response;
|
|
// self.result.response = response;
|
|
|
completionHandler(NSURLSessionResponseAllow);
|
|
completionHandler(NSURLSessionResponseAllow);
|
|
|
|
|
|
|
@@ -109,7 +205,13 @@
|
|
|
|
|
|
|
|
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
|
|
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
|
|
|
|
|
|
|
|
- [self.recvData appendData:data];
|
|
|
|
|
|
|
+ if (!self.isDownloadTask) {
|
|
|
|
|
+ [self.recvData appendData:data];
|
|
|
|
|
+ } else {
|
|
|
|
|
+
|
|
|
|
|
+ [self.fileStream jl_writeData:data];
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
@end
|