zoukankan      html  css  js  c++  java
  • wifi文件传输

    步骤:

    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为例:

    //    NSString* uploadDirPath = [[config documentRoot] stringByAppendingPathComponent:@"upload"];
        NSString *uploadDirPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

    6.在适当的地方配置server启动,这里在ViewController中操作

    #import "ViewController.h"
    #import "HTTPServer.h"
    #import "DDLog.h"
    #import "DDTTYLogger.h"
    #import "MyHTTPConnection.h"
    // 获取IP地址
    #include <arpa/inet.h>
    #include <ifaddrs.h>
    
    @interface ViewController (){
        HTTPServer *httpServer;
    }
    
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        httpServer = [[HTTPServer alloc] init];
        [httpServer setType:@"_http._tcp."];
        // webPath是server搜寻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地址
        [self getIpAddresses];
    
    }
    
    // 获取局域网IP地址
    - (NSString *)getIpAddresses {
        NSString *address = @"error";
        struct ifaddrs *interfaces = NULL;
        struct ifaddrs *temp_addr = NULL;
        int success = 0;
        // retrieve the current interfaces - returns 0 on success
        success = getifaddrs(&interfaces);
        if (success == 0)
        {
            // Loop through linked list of interfaces
            temp_addr = interfaces;
            while(temp_addr != NULL)
            {
                if(temp_addr->ifa_addr->sa_family == AF_INET)
                {
                    // Check if interface is en0 which is the wifi connection on the iPhone
                    if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])
                    {
                        // Get NSString from C String
                        address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                    }
                }
                temp_addr = temp_addr->ifa_next;
            }
        }
        // Free memory
        freeifaddrs(interfaces);
        
        NSLog(@"%@", address);
        
        return address;
    }
    
    // 查询手机里面是否已经导入文件成功
    - (IBAction)clicked {
        
        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);
            }
        }
    
    }
    
    @end

    开始运行代码,测试时一定要在真机上运行,会打印出如下:

    7.运行后,在浏览器输入:“ip:port” 访问即可,如果成功,会看到如下页面:

    选择一个文件点击:“Submit”上传,如下图:

    8.点击app里的按钮调用:“- (IBAction)clicked” 方法,如果上传成功,会打印如下图:

    我们看到了:“text.txt” 文件就是刚从浏览器上传到手机里的文件,“Snapshots” 文件是系统本来就有的文件。

    我想:如果两台手机连接在同一局域网,另一台手机用浏览器输入“ip:port”这个地址,应该也可以通过手机上传文件到手机。

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

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

    本文参考:http://blog.csdn.net/happyshaotang2/article/details/51433143。谢谢作者。

  • 相关阅读:
    windows上安装xampp和dvwa
    CentOS 7上安装Vtiger CRM Open Source Edition
    快速建站(lnmp)
    centos7中jdk安装
    centos7中apache安装
    centos7中mysql安装
    再探决策树算法之利用sklearn进行决策树实战
    决策树算法初探
    定制化自己的itchat
    itchat初探
  • 原文地址:https://www.cnblogs.com/pengyunjing/p/5951040.html
Copyright © 2011-2022 走看看