首页 技术 正文
技术 2022年11月14日
0 收藏 331 点赞 2,410 浏览 3045 个字

步骤:

1.下载CocoaHTTPServer 
2.解压后,将CocoaHTTPServer-master目录下的Core导入工程。 
3.打开Samples/SimpleFileUploadServer,将其中的MyHTTPConnection类文件、web文件夹导入工程。 
4.打开Vendor,将其中的CocoaAsyncSocket、CocoaLumberjack文件夹导入。 
所有要导入的文件如下图所示:注意,其中的HYBIPHelper为获取本地ip的工具类,不是必要的,请忽视。

wifi文件传输

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 = ;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == )
{
// 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:];
if ([fileManager fileExistsAtPath:cacheDirectory]) {
NSArray *childerFiles=[fileManager subpathsAtPath:cacheDirectory];
for (NSString *fileName in childerFiles) {
// 文件名(本地文件视频名),用于界面展示
NSLog(@"fileName名 %@", fileName);
}
}}@end

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

wifi文件传输

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

wifi文件传输

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

wifi文件传输

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

wifi文件传输

我们看到了:“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。谢谢作者。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,087
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,562
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,821
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905