// 获取MIMEType
//1. 发送请求,可以在响应头(内部有MIMEType)
//2. 百度 MIMEType
//3. 调用C语言API
//4. application/octet-stream 任意的二进制数据类型
// Created by 朝阳 on 2017/12/12.
// Copyright © 2017年 sunny. All rights reserved.
//
import "ViewController.h"
import <MobileCoreServices/MobileCoreServices.h>
@interface ViewController ()
@end
@implementation ViewController
-
(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 获取MIMEType
//1. 发送请求,可以在响应头(内部有MIMEType)
//2. 百度 MIMEType
//3. 调用C语言API
//4. application/octet-stream 任意的二进制数据类型// [self getMimeType];
NSString *mimeType = [self mimeTypeForFileAtPath:@"/Users/sunny/Desktop/test.h"];
NSLog(@"%@",mimeType);
}
// 发送请求,在响应头中 有MIMEType属性
-
(void)getMimeType
{
//1. url
// NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login"];
NSURL *url = [NSURL fileURLWithPath:@"/Users/sunny/Desktop/test.h"];//2. 创建请求对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//3. 发送异步请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//4. 获得文件的类型
NSLog(@"%@",response.MIMEType);
}];
}
// 调用C语言API
-
(NSString *)mimeTypeForFileAtPath:(NSString *)path
{
if (![[[NSFileManager alloc] init] fileExistsAtPath:path]) {
return nil;
}CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)[path pathExtension], NULL);
CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType);
CFRelease(UTI);
if (!MIMEType) {
return @"application/octet-stream";
}
return (__bridge NSString *)(MIMEType);
}
网友评论