iOS 文件上传 post数据

//联系人:石虎QQ: 1224614774昵称:嗡嘛呢叭咪哄

一、文件下载

获取资源文件大小有两张方式

1、

[objc]view plaincopy

HTTP HEAD方法

NSMutableURLRequest*request = [NSMutableURLRequestrequestWithURL:urlcachePolicy:0timeoutInterval:kTimeout];

request.HTTPMethod=@"HEAD";

[NSURLConnectionsendAsynchronousRequest:requestqueue:self.myQueuecompletionHandler:^(NSURLResponse*response,NSData*data,NSError*connectionError) {

NSLog(@"%@", response);

NSLog(@"---------------");

NSLog(@"%@", data);

}];

运行测试代码可以发现,HEAD方法只是返回资源信息,而不会返回数据体

应用场景:

获取资源Mimetype

获取资源文件大小,用于端点续传或多线程下载

2

[objc]view plaincopy

使用块代码获取网络资源大小的方法

- (void)fileSizeWithURL:(NSURL*)urlcompletion:(void(^)(longlongcontentLength))completion

{

NSMutableURLRequest*request = [NSMutableURLRequestrequestWithURL:urlcachePolicy:0timeoutInterval:kTimeout];

request.HTTPMethod=@"HEAD";

NSURLResponse*response =nil;

[NSURLConnectionsendSynchronousRequest:requestreturningResponse:&responseerror:NULL];

completion(response.expectedContentLength);

}

确定每次下载数据包的伪代码实现

[objc]view plaincopy

- (void)downloadFileWithURL:(NSURL*)url

{

[selffileSizeWithURL:urlcompletion:^(longlongcontentLength) {

NSLog(@"文件总大小:%lld", contentLength);

// 根据大小下载文件

while(contentLength > kDownloadBytes) {

NSLog(@"每次下载长度:%lld", (longlong)kDownloadBytes);

contentLength -= kDownloadBytes;

}

NSLog(@"最后下载字节数:%lld", contentLength);

}];

}

HTTP Range的示例

通过设置Range可以指定每次从网路下载数据包的大小

Range示例

bytes=0-499从0到499的头500个字节

bytes=500-999从500到999的第二个500字节

bytes=500-从500字节以后的所有字节

bytes=-500最后500个字节

bytes=500-599,800-899同时指定几个范围

Range小结

-用于分隔

前面的数字表示起始字节数

后面的数组表示截止字节数,没有表示到末尾

,用于分组,可以一次指定多个Range,不过很少用

[objc]view plaincopy

分段Range代码实现

longlongfromBytes =0;

longlongtoBytes =0;

while(contentLength > kDownloadBytes) {

toBytes = fromBytes + kDownloadBytes -1;

NSString*range = [NSStringstringWithFormat:@"bytes=%lld-%lld", fromBytes,toBytes];

NSLog(@"range %@", range);

fromBytes += kDownloadBytes;

contentLength -= kDownloadBytes;

}

fromBytes = fromBytes + contentLength -1;

NSString*range = [NSStringstringWithFormat:@"bytes=%lld-%lld", fromBytes,toBytes];

NSLog(@"range %@", range);

[objc]view plaincopy

分段下载文件

NSMutableURLRequest*request = [NSMutableURLRequestrequestWithURL:urlcachePolicy:NSURLRequestReloadIgnoringCacheDatatimeoutInterval:kTimeout];

NSString*range = [NSStringstringWithFormat:@"bytes=%lld-%lld", from,end];

[requestsetValue:rangeforHTTPHeaderField:@"Range"];

NSURLResponse*response =nil;

NSData*data = [NSURLConnectionsendSynchronousRequest:requestreturningResponse:&responseerror:NULL];

NSLog(@"%@-%@-%ld", range, response, (unsignedlong)data.length);

提示:

如果GET包含Range请求头,响应会以状态码206(PartialContent)返回而不是200(OK)

[objc]view plaincopy

将数据写入文件

// 打开缓存文件

NSFileHandle*fp = [NSFileHandlefileHandleForWritingAtPath:self.cachePath];

// 如果文件不存在,直接写入数据

if(!fp) {

[datawriteToFile:self.cachePathatomically:YES];

}else{

// 移动到文件末尾

[fpseekToEndOfFile];

// 将数据文件追加到文件末尾

[fpwriteData:data];

// 关闭文件句柄

[fpcloseFile];

}

[objc]view plaincopy

检查文件大小

// 判断文件是否存在

if([[NSFileManagerdefaultManager]fileExistsAtPath:self.cachePath]) {

NSDictionary*dict = [[NSFileManagerdefaultManager]attributesOfItemAtPath:self.cachePatherror:NULL];

return[dict[NSFileSize]longLongValue];

}else{

return0;

}

提示:由于数据是追加的,为了避免重复从网络下载文件,在下载之前

判断缓存路径中文件是否已经存在

如果存在检查文件大小

如果文件大小与网络资源大小一致,则不再下载

全部代码如下

[objc]view plaincopy

//  01.文件下载

//

#import "MJViewController.h"

#import "FileDownload.h"

@interfaceMJViewController ()

@property(nonatomic,strong)FileDownload*download;

@property(weak,nonatomic) IBOutletUIImageView*imageView;

@end

@implementationMJViewController

- (void)viewDidLoad

{

[superviewDidLoad];

self.download= [[FileDownloadalloc]init];

[self.downloaddownloadFileWithURL:[NSURLURLWithString:@"http://localhost/itcast/images/head4.png"]completion:^(UIImage*image) {

self.imageView.image= image;

}];

}

@end

[objc]view plaincopy

//

//  FileDownload.m

//  01.文件下载.

//

#import "FileDownload.h"

#import "NSString+Password.h"

#define kTimeOut        2.0f

// 每次下载的字节数

#define kBytesPerTimes  20250

@interfaceFileDownload()

@property(nonatomic,strong)NSString*cacheFile;

@property(nonatomic,strong)UIImage*cacheImage;

@end

@implementationFileDownload

/**

为了保证开发的简单,所有方法都不使用多线程,所有的注意力都保持在文件下载上

在开发中如果碰到比较绕的计算问题时,建议:

1> 测试数据不要太大

2> 测试数据的数值变化,能够用笔算计算出准确的数值

3> 编写代码对照测试

*/

//- (NSString *)cacheFile

//{

//    if (!_cacheFile) {

//        NSString *cacheDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];

//        _cacheFile = [cacheDir stringByAppendingPathComponent:@"123.png"];

//    }

//    return _cacheFile;

//}

- (UIImage*)cacheImage

{

if(!_cacheImage) {

_cacheImage = [UIImageimageWithContentsOfFile:self.cacheFile];

}

return_cacheImage;

}

- (void)setCacheFile:(NSString*)urlStr

{

NSString*cacheDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES)[0];

urlStr = [urlStrMD5];

_cacheFile = [cacheDirstringByAppendingPathComponent:urlStr];

}

- (void)downloadFileWithURL:(NSURL*)urlcompletion:(void(^)(UIImage*image))completion

{

// GCD中的串行队列异步方法

dispatch_queue_t q = dispatch_queue_create("cn.itcast.download", DISPATCH_QUEUE_SERIAL);

dispatch_async(q, ^{

NSLog(@"%@", [NSThreadcurrentThread]);

// 把对URL进行MD5加密之后的结果当成文件名

self.cacheFile= [urlabsoluteString];

// 1. 从网络下载文件,需要知道这个文件的大小

longlongfileSize = [selffileSizeWithURL:url];

// 计算本地缓存文件大小

longlongcacheFileSize = [selflocalFileSize];

if(cacheFileSize == fileSize) {

dispatch_async(dispatch_get_main_queue(), ^{

completion(self.cacheImage);

});

NSLog(@"文件已经存在");

return;

}

// 2. 确定每个数据包的大小

longlongfromB =0;

longlongtoB =0;

// 计算起始和结束的字节数

while(fileSize > kBytesPerTimes) {

// 20480 + 20480

//

toB = fromB + kBytesPerTimes -1;

// 3. 分段下载文件

[selfdownloadDataWithURL:urlfromB:fromBtoB:toB];

fileSize -= kBytesPerTimes;

fromB += kBytesPerTimes;

}

[selfdownloadDataWithURL:urlfromB:fromBtoB:fromB + fileSize -1];

dispatch_async(dispatch_get_main_queue(), ^{

completion(self.cacheImage);

});

});

}

#pragma mark 下载指定字节范围的数据包

/**

NSURLRequestUseProtocolCachePolicy = 0,        // 默认的缓存策略,内存缓存

NSURLRequestReloadIgnoringLocalCacheData = 1,  // 忽略本地的内存缓存

NSURLRequestReloadIgnoringCacheData

*/

- (void)downloadDataWithURL:(NSURL*)urlfromB:(longlong)fromBtoB:(longlong)toB

{

NSLog(@"数据包:%@", [NSThreadcurrentThread]);

NSMutableURLRequest*request = [NSMutableURLRequestrequestWithURL:urlcachePolicy:NSURLRequestReloadIgnoringCacheDatatimeoutInterval:kTimeOut];

// 指定请求中所要GET的字节范围

NSString*range = [NSStringstringWithFormat:@"Bytes=%lld-%lld", fromB,toB];

[requestsetValue:rangeforHTTPHeaderField:@"Range"];

NSLog(@"%@", range);

NSURLResponse*response =nil;

NSData*data = [NSURLConnectionsendSynchronousRequest:requestreturningResponse:&responseerror:NULL];

// 写入文件,覆盖文件不会追加

//    [data writeToFile:@"/Users/aplle/Desktop/1.png" atomically:YES];

[selfappendData:data];

NSLog(@"%@", response);

}

#pragma mark - 读取本地缓存文件大小

- (longlong)localFileSize

{

// 读取本地文件信息

NSDictionary*dict = [[NSFileManagerdefaultManager]attributesOfItemAtPath:self.cacheFileerror:NULL];

NSLog(@"%lld", [dict[NSFileSize]longLongValue]);

return[dict[NSFileSize]longLongValue];

}

#pragma mark - 追加数据到文件

- (void)appendData:(NSData*)data

{

// 判断文件是否存在

NSFileHandle*fp = [NSFileHandlefileHandleForWritingAtPath:self.cacheFile];

// 如果文件不存在创建文件

if(!fp) {

[datawriteToFile:self.cacheFileatomically:YES];

}else{

// 如果文件已经存在追加文件

// 1> 移动到文件末尾

[fpseekToEndOfFile];

// 2> 追加数据

[fpwriteData:data];

// 3> 写入文件

[fpcloseFile];

}

}

#pragma mark - 获取网络文件大小

- (longlong)fileSizeWithURL:(NSURL*)url

{

// 默认是GET

NSMutableURLRequest*request = [NSMutableURLRequestrequestWithURL:urlcachePolicy:0timeoutInterval:kTimeOut];

// HEAD 头,只是返回文件资源的信息,不返回具体是数据

// 如果要获取资源的MIMEType,也必须用HEAD,否则,数据会被重复下载两次

request.HTTPMethod=@"HEAD";

// 使用同步方法获取文件大小

NSURLResponse*response =nil;

[NSURLConnectionsendSynchronousRequest:requestreturningResponse:&responseerror:NULL];

// expectedContentLength文件在网络上的大小

NSLog(@"%lld", response.expectedContentLength);

returnresponse.expectedContentLength;

}

@end

二、文件上传

代码如下

[objc]view plaincopy

//

//  MJViewController.m

//  02.Post上传

//

#import "MJViewController.h"

#import "UploadFile.h"

@interfaceMJViewController ()

@end

@implementationMJViewController

- (void)viewDidLoad

{

[superviewDidLoad];

UploadFile*upload = [[UploadFilealloc]init];

NSString*urlString =@"http://localhost/upload.php";

NSString*path = [[NSBundlemainBundle]pathForResource:@"头像1.png"ofType:nil];

NSData*data = [NSDatadataWithContentsOfFile:path];

[uploaduploadFileWithURL:[NSURLURLWithString:urlString]data:data];

}

@end

[objc]view plaincopy

//

//  UploadFile.m

//  02.Post上传

//

#import "UploadFile.h"

@implementationUploadFile

// 拼接字符串

staticNSString*boundaryStr =@"--";// 分隔字符串

staticNSString*randomIDStr;// 本次上传标示字符串

staticNSString*uploadID;// 上传(php)脚本中,接收文件字段

- (instancetype)init

{

self= [superinit];

if(self) {

randomIDStr =@"itcast";

uploadID =@"uploadFile";

}

returnself;

}

#pragma mark - 私有方法

- (NSString*)topStringWithMimeType:(NSString*)mimeTypeuploadFile:(NSString*)uploadFile

{

NSMutableString*strM = [NSMutableStringstring];

[strMappendFormat:@"%@%@\n", boundaryStr,randomIDStr];

[strMappendFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\n", uploadID,uploadFile];

[strMappendFormat:@"Content-Type: %@\n\n",mimeType];

NSLog(@"%@", strM);

return[strMcopy];

}

- (NSString*)bottomString

{

NSMutableString*strM = [NSMutableStringstring];

[strMappendFormat:@"%@%@\n", boundaryStr,randomIDStr];

[strMappendString:@"Content-Disposition: form-data; name=\"submit\"\n\n"];

[strMappendString:@"Submit\n"];

[strMappendFormat:@"%@%@--\n", boundaryStr,randomIDStr];

NSLog(@"%@", strM);

return[strMcopy];

}

#pragma mark - 上传文件

- (void)uploadFileWithURL:(NSURL*)urldata:(NSData*)data

{

// 1> 数据体

NSString*topStr = [selftopStringWithMimeType:@"image/png"uploadFile:@"头像1.png"];

NSString*bottomStr = [selfbottomString];

NSMutableData*dataM = [NSMutableDatadata];

[dataMappendData:[topStrdataUsingEncoding:NSUTF8StringEncoding]];

[dataMappendData:data];

[dataMappendData:[bottomStrdataUsingEncoding:NSUTF8StringEncoding]];

// 1. Request

NSMutableURLRequest*request = [NSMutableURLRequestrequestWithURL:urlcachePolicy:0timeoutInterval:2.0f];

// dataM出了作用域就会被释放,因此不用copy

request.HTTPBody= dataM;

// 2> 设置Request的头属性

request.HTTPMethod=@"POST";

// 3> 设置Content-Length

NSString*strLength = [NSStringstringWithFormat:@"%ld", (long)dataM.length];

[requestsetValue:strLengthforHTTPHeaderField:@"Content-Length"];

// 4> 设置Content-Type

NSString*strContentType = [NSStringstringWithFormat:@"multipart/form-data; boundary=%@",randomIDStr];

[requestsetValue:strContentTypeforHTTPHeaderField:@"Content-Type"];

// 3> 连接服务器发送请求

[NSURLConnectionsendAsynchronousRequest:requestqueue:[[NSOperationQueuealloc]init]completionHandler:^(NSURLResponse*response,NSData*data,NSError*connectionError) {

NSString*result = [[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding];

NSLog(@"%@", result);

}];

}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • iOS开发系列--网络开发 概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博、微信等,这些应用本身可...
    lichengjin阅读 3,721评论 2 7
  • 1.不可变数组转变为可变数组声明实例变量的数组 必须记得实现 对于遍历数组找到对象后 如果还需要查找 记得先结束 ...
    小新xin阅读 759评论 0 1
  • 使用NSURLConnection实现下载 1. 小文件下载 第一种方式(NSData) 第二种方式(NSURLC...
    搁浅的青蛙阅读 1,980评论 3 10
  • 小文件下载如果文件比较小,下载方式会比较多直接用NSData的+ (id)dataWithContentsOfUR...
    醉叶惜秋阅读 891评论 0 0
  • 原文链接:AFNetworking速成教程 本文是由 iOS Tutorial小组成员ScottSherwood撰...
    默默_David阅读 2,433评论 0 4