GetURLFileLength.h 文件
/**
* @brief 通過網(wǎng)絡(luò)url獲得文件的大小
*/
@interface GetURLFileLength : NSObject<NSURLConnectionDataDelegate>
typedef void(^FileLength)(long long length, NSError *error);
@property (nonatomic, copy) FileLength block;
/**
* 通過url獲得網(wǎng)絡(luò)的文件的大小 返回byte
*
* @param url 網(wǎng)絡(luò)url
*
* @return 文件的大小 byte
*/
- (void)getUrlFileLength:(NSString *)url withResultBlock:(FileLength)block;
GetURLFileLength.m 文件
@implementation GetURLFileLength
/**
* 通過url獲得網(wǎng)絡(luò)的文件的大小 返回byte
*
* @param url 網(wǎng)絡(luò)url
*
* @return 文件的大小 byte
*/
- (void)getUrlFileLength:(NSString *)url withResultBlock:(FileLength)block
{
_block = block;
NSMutableURLRequest *mURLRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[mURLRequest setHTTPMethod:@"HEAD"];
mURLRequest.timeoutInterval = 5.0;
NSURLConnection *URLConnection = [NSURLConnection connectionWithRequest:mURLRequest delegate:self];
[URLConnection start];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSDictionary *dict = [(NSHTTPURLResponse *)response allHeaderFields];
NSNumber *length = [dict objectForKey:@"Content-Length"];
[connection cancel];
if (_block) {
_block([length longLongValue], nil); // length單位是byte豺裆,除以1000后是KB(文件的大小計算好像都是1000,而不是1024)号显,
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"獲取文件大小失敵舨隆:%@",error);
if (_block) {
_block(0, error);
}
[connection cancel];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
當(dāng)中獲得到頭信息中 dict包含的信息可以通過解析獲得,打印的dict內(nèi)容如下:
Printing description of dict:
{
"Accept-Ranges" = bytes;
"Access-Control-Allow-Origin" = "*";
"Access-Control-Expose-Headers" = "X-Log, X-Reqid";
"Access-Control-Max-Age" = 2592000;
Age = 1;
"Cache-Control" = "public, max-age=31536000";
Connection = "keep-alive";
"Content-Disposition" = "inline; filename=\"4587945932505.mp4\"";
"Content-Length" = 1149810; // 文件的大小
"Content-Transfer-Encoding" = binary;
"Content-Type" = "video/mp4"; // 文件的類型
Date = "Fri, 05 Aug 2016 01:55:16 GMT";
Etag = "\"Fo5QDSpIMMLcV2W2fH899FH4q********\"";
"Last-Modified" = "Fri, 22 Jul 2016 01:46:05 GMT"; // 最近一次修改時間
Server = nginx;
"X-Log" = "mc.g;IO:2";
"X-Qiniu-Zone" = 0;
"X-Reqid" = vHoAANGi1Q6OxmcU;
"X-Via" = "1.1 suydong34:0 (Cdn Cache Server V2.0), 1.1 yd70:4 (Cdn Cache Server V2.0)";
}
如果要獲取本地文件的文件的信息(文件大小)押蚤,同樣可以在GetURLFileLength.h 文件中
/**
* 通過文件的名字以及創(chuàng)建的時間獲得文件的大小 返回byte
*
* @param fileName 文件名字
* @param created 文件在服務(wù)器的創(chuàng)建時間 通過創(chuàng)建的時間建立的目錄文件
* @param block length:文件長度蔑歌, status:是否已經(jīng)下載 返回 已下載,未下載
* @return
*/
- (void)getLocalPathFileLength:(NSString *)fileName created:(UInt32)created withResultBlock:(void(^)(long long length, NSString *status))block;
GetURLFileLength.m 文件
- (void)getLocalPathFileLength:(NSString *)fileName created:(UInt32)created withResultBlock:(void(^)(long long length, NSString *status))block
{
// 判斷是否已經(jīng)離線下載了
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
// filepath是通過一定的方式拼接的揽碘,我的文件一般存在于documents下的LOCAL_SAVE_PATH當(dāng)中次屠,然后以created作為文件的目錄保存园匹,所以完整的路徑拼接即如下:
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%d/%@", LOCAL_SAVE_PATH, created, fileName]];
NSFileManager *filemanager = [NSFileManager defaultManager];
if ([filemanager fileExistsAtPath:path]) {
long long length = [[filemanager attributesOfItemAtPath:path error:nil] fileSize];
if (block) {
block(length, @"已下載");
}
} else {
if (block) {
block(0, @"未下載");
}
}
}
其他信息也可以如下獲取
/**
* 通過文件的路徑獲得本地文件的基本信息
*
* @param filePath <#filePath description#>
* @param block <#block description#>
*/
- (void)getFileInfoByFilePath:(NSString *)filePath WithBlock:(void(^)(long long fileSize, NSDate *modificationtime, NSError *error))block
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:filePath error:&error];
if (fileAttributes != nil) {
NSNumber *fileSize = [fileAttributes objectForKey:NSFileSize];
NSDate *fileModDate = [fileAttributes objectForKey:NSFileModificationDate]; // 最近一次的修改時間
if (block) {
block([fileSize unsignedLongLongValue], fileModDate, nil);
}
} else {
if (block) {
block(0, nil, [NSError errorWithDomain:@"文件路徑出錯" code:100 userInfo:nil]);
}
}
}