[绍棠] 利用CocoaHTTPServer实现WIFI局域网传输文件到iPhone, 实现文件上传

近日在做一个视频文件上传,其中涉及到WIFI局域网文件上传和iTunes文件导入,通过一周多的研究, 终于实现的该功能,我是用的框架是CocoaHTTPServer。具体原理是将手机作为服务器, 只要电脑和移动设备连入同一热点,即可使用电脑访问iOS服务器的页面,利用POST实现文件的上传,从而与网页端实现文件互传

实现

CocoaHTTPServer没有现成的向iOS设备传输的Sample,按照下面的步骤配置即可。

1.下载CocoaHTTPServer 
2.解压后,将CocoaHTTPServer-master目录下的Core导入工程。 
3.打开Samples/SimpleFileUploadServer,将其中的MyHTTPConnection类文件、web文件夹导入工程。 
4.打开Vendor,将其中的CocoaAsyncSocket、CocoaLumberjack文件夹导入。 
所有要导入的文件如下图所示:注意,其中的HYBIPHelper为获取本地ip的工具类,不是必要的,请忽视。 
所有要导入的文件 
5.打开工程,打开MyHTTPConnection.m,根据标记#pragma mark multipart form data parser delegate跳转或者直接找到139行,- (void) processStartOfPartWithHeader:(MultipartMessageHeader*) header方法,将其中filePath的值修改为iOS的某个目录,这个路径是上传的文件存储的路径,

我们可以已Caches为例:

1. <code class="language-Objective-C hljs objectives">//  NSString* uploadDirPath = [[config documentRoot] stringByAppendingPathComponent:@"upload"];
2. NSString *uploadDirPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];</code>

6.在适当的地方配置server启动,这里以AppDelegate为例:

#import "ViewController.h"

#import "HTTPServer.h"

#import "DDLog.h"

#import "DDTTYLogger.h"

#import "MyHTTPConnection.h"


.

// 代码实现部分

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    NSFileManager *fileManager=[NSFileManager defaultManager];

    NSArray *pathcaches = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);

    NSString* cacheDirectory = [pathcaches objectAtIndex:0];

    if ([fileManager fileExistsAtPath:cacheDirectory]) {

        NSArray *childerFiles=[fileManager subpathsAtPath:cacheDirectory];

        for (NSString *fileName in childerFiles) {

            // 文件名(本地文件视频名),用于界面展示

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

        }

    }


    

    self.view.backgroundColor = [UIColor whiteColor];

    httpServer = [[HTTPServer alloc] init];

    [httpServer setType:@"_http._tcp."];

    // webPathserver搜寻HTML等文件的路径

    NSString *webPath = [[NSBundle mainBundle] resourcePath];

    [httpServer setDocumentRoot:webPath];

    [httpServer setConnectionClass:[MyHTTPConnection class]];

    NSError *err;

    if ([httpServer start:&err]) {

        NSLog(@"port %hu",[httpServer listeningPort]);

    }else{

        NSLog(@"%@",err);

    }

    // 获取ip地址, 可以看我昨天的博客(http://blog.csdn.net/happyshaotang2/article/details/51424450)

    NSString *ipStr = [self getIpAddresses];

    NSLog(@"ip地址   %@", ipStr);

    // 在AppDelegate建两个属性ipStr和postStr, 便于传值, 害怕别的界面会用到

    AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;

    tAppDel.ipStr = ipStr;

    tAppDel.postStr = [NSString stringWithFormat:@"%hu", [httpServer listeningPort]];

    

}


07.

7.运行后,在控制台打印出端口号,再通过路由器或者热点工具查询设备的内网ip,通过ip:port访问即可,如果成功,会看到如下页面: 
默认的文件传输页 
8.如果上传成功,在控制台会打印存储到的位置,可以通过打开沙盒来验证。


补充:iTunes实现文件共享, 可以参考 http://blog.csdn.net/happyshaotang2/article/details/51394538    

另外, 想修改文件中的的两个HTML文件, 主要是在MyHTTPConnection.m 文件中修改, 通过判断path来修改对应的Response, 其实就是服务器发送请求, 你手机端在收到请求后返回对应的数据类型就行了。暂时就这么多, 谢谢阅读





猜你喜欢

转载自blog.csdn.net/happyshaotang2/article/details/51433143
今日推荐