AFNetworking 文件断点上传详细解读

本文介绍了一种文件断点上传的方法,通过将大文件分割为多个小块并逐一上传,最终在服务器端合并成完整文件。文章提供了Objective-C客户端实现代码和服务端Spring MVC整合示例。

前段时间,我写过一篇关于AFNetworking文件断点下载 的文章,我今天为啥子又要写断点上传了?

主要是因为别人写的太坑爹了,很难理解,木有办法。。。。我必须的自己理解下

断点上传其实跟断点下载是类似的,也是把文件分成若干份;那么很多人就觉得奇怪,为什么不一次性了?小文件可以一次性的上传,但是如果这个文件是上百兆甚至更多显然一次性上传肯定有问题的 ,而且我们使用的是HTTP(S)连接,并不是所谓的长连接 


先说下思路,在客户端上传的时候把文件分成若干份


再次引用上次的图 ,把文件分成若干块

  NSString *ps = [P stringByAppendingPathComponent:@"bz.jpg"];
    NSData* imgData =[[NSData alloc] initWithContentsOfFile:ps];
    
    NSUInteger totalLength = [imgData length];
    
    NSUInteger minBlock = 10*1024;
    
    NSUInteger count = totalLength/minBlock +(totalLength%minBlock?1:0);
    
    NSUInteger lastIndexLength = totalLength - (count -1)*minBlock;
    
    NSMutableArray *list = [[NSMutableArray alloc] initWithCapacity:count];
    
    for(NSUInteger i =0; i<count-1; i++){
 
        NSData *d = [imgData subdataWithRange:NSMakeRange(i*(minBlock), minBlock)];
        [list addObject:d];
    }
    
    NSData *d = [imgData subdataWithRange:NSMakeRange((count -1)*minBlock, lastIndexLength)];
     [list addObject:d];

    NSMutableData *mutableData = [[NSMutableData alloc] init];
    for (NSData *d in list) {
        [mutableData appendData:d];
    }
    UIImage *img = [[UIImage alloc] initWithData:mutableData];

为了验证我们分的块是否能组合成一个新文件,我们把块重新结合在一起, 注意顺序,这个很重要,然后我们通过xcode断点看下我们的文件是否正确, 在此强烈推荐使用模拟器,因为Apple在真机上操作文件是相当蛋疼的。。。 ‘

我们来张图验证验证下



这是模拟器中的图片,然后经过分割拼接操作






我们可以看到,图片拼接之后文件是完整的 

那么我们需要的是将图片的每一段分别上传到服务器上去,上传成功我们做一个标记,上传失败我们也做一个标记。。。 废话不读说,我直接贴上objective-c的代码

#import "UpLoadViewController.h"

#import "AFNetworking.h"



#define P @"/Users/apple/Library/Developer/CoreSimulator/Devices/68EC2F50-5773-4CC7-8635-BAC29620349B/data/Containers/Data/Application/8B5DF169-DC25-45AA-B532-333D4D9C1812/Library/Caches/download/org"

#define URL @"http://10.0.1.4:8778/hb/user/uploaddd.do"

@interface UpLoadViewController (){
    
    NSURLSessionDataTask *task;
}

@end

@implementation UpLoadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    UIButton *button1 = [[UIButton alloc ] init];
    button1.frame = CGRectMake(10, 140, 80, 100);
    [button1 setTitle:@"断点文件上传" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(button1) forControlEvents:UIControlEventTouchUpInside];
    button1.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:button1];

    
}

-(void)button1{
    
 
    
    NSString *ps = [P stringByAppendingPathComponent:@"bz.jpg"];
    NSData* imgData =[[NSData alloc] initWithContentsOfFile:ps];
    
    NSUInteger totalLength = [imgData length];
    
    NSUInteger minBlock = 10*1024;
    
    NSUInteger count = totalLength/minBlock +(totalLength%minBlock?1:0);
    
    NSUInteger lastIndexLength = totalLength - (count -1)*minBlock;
    
    NSMutableArray *list = [[NSMutableArray alloc] initWithCapacity:count];
    
    for(NSUInteger i =0; i<count-1; i++){
 
        NSData *d = [imgData subdataWithRange:NSMakeRange(i*(minBlock), minBlock)];
        [list addObject:d];
    }
    
    NSData *d = [imgData subdataWithRange:NSMakeRange((count -1)*minBlock, lastIndexLength)];
     [list addObject:d];

    NSMutableData *mutableData = [[NSMutableData alloc] init];
    for (NSData *d in list) {
        [mutableData appendData:d];
    }
    UIImage *img = [[UIImage alloc] initWithData:mutableData];
    
    
    dispatch_group_t group = dispatch_group_create();
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    dispatch_queue_t queue=dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
    for (int i =0; i<[list count]  ; i++) {
        
        
         dispatch_group_async(group, queue, ^{
         NSData *d = list[i];
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        [manager POST:@"http://10.0.1.4:8778/hb/user/uploaddd.do" parameters:@{@"tmpId":@(i)} constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            
            [formData appendPartWithFileData:d name:@"uploadFile" fileName:@"uploadFile" mimeType:@"application/octet-stream"];
            
            
        } progress:^(NSProgress * _Nonnull uploadProgress) {
            
            NSLog(@"%lld     ------ %lld",uploadProgress.totalUnitCount,uploadProgress.completedUnitCount);
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSLog(@"%d",i);//顺序打印
            dispatch_semaphore_signal(semaphore);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%d",i);//顺序打印
            dispatch_semaphore_signal(semaphore);
            
        }];
 
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
             
        });
    }
    
    
    
    dispatch_group_notify(group, queue, ^{
        //所有请求返回数据后执行
    });
    
    
}
@end


这是客户端的代码, 也就是传个服务端的不是整个文件而是文件的片段,那么在服务端需要做的是接收文件片段并且把文件片段连接成一个文件服务端我使用的是简单的springMVC 写了一个接口;

@RequestMapping("/uploaddd")
	public void uploadModelImageDD(@RequestParam("uploadFile") MultipartFile uploadFile,
			@RequestParam("tmpId") Long tmpId, HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		InputStream inputStream = uploadFile.getInputStream();

		System.out.println("tmpId = " + tmpId);
		System.out.println("size = " + uploadFile.getSize());
		System.out.println("name = " + uploadFile.getName());
		System.out.println("ContentType = " + uploadFile.getContentType());
		System.out.println("OriginalFilename = " + uploadFile.getOriginalFilename());
		System.out.println("----------------------------------------------");
		System.out.println("\n\r");
		System.out.println("\n\r");
		File file = new File("/Development/appupload/" + tmpId + ".part");
		FileOutputStream fos = new FileOutputStream(file);
		
		int ch = 0;
		while ((ch = inputStream.read()) != -1) {
			fos.write(ch);
		}
		fos.close();

		if (tmpId == 74) {

			List<FileInputStream> list = new ArrayList<FileInputStream>();
			File tempFile = new File("/Development/appupload");

			for (int i = 0; i < 75; i++) {
				list.add(new FileInputStream(new File(tempFile, i + ".part")));
			}
			// 使用 Enumeration(列举) 将文件全部列举出来
			Enumeration<FileInputStream> eum = Collections.enumeration(list);
			// SequenceInputStream合并流 合并文件
			SequenceInputStream sis = new SequenceInputStream(eum);
			fos = new FileOutputStream(new File("/Development/appupload", "aaa.jpg"));
			byte[] by = new byte[100];
			int len;
			while ((len = sis.read(by)) != -1) {
				fos.write(by, 0, len);
			}
			fos.flush();
			fos.close();
			sis.close();
		}

	}


我来解释下 if (tmpId == 74)  这是什么意思,其实我是把文件分成74端,然后当文件接收完成的时候通知服务端合并,其实如果你自己写的话需要按照具体的情况



这是服务端输出的日志,每个片段上传成功就会输出信息,然后看下我接收片段的文件夹,你可以看到我合成成功了



以上是图片,我们看下其他的文件,我来一个zip文件搞一搞



ZIP文件也是可以的 ,也就是说这种方法断点上传是可行的 。。 






评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值