标签:
1
|
# import "GCDAsyncSocket.h" |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
import UIKit import AssetsLibrary class ViewController : UIViewController { //文件操作类 var manager: NSFileManager ! //保存用户文档目录 var docPath: NSURL ! override func viewDidLoad() { super .viewDidLoad() // Do any additional setup after loading the view, typically from a nib. //设置用户文档路径 manager = NSFileManager .defaultManager() let urlsForDocDirectory = manager. URLsForDirectory ( NSSearchPathDirectory . DocumentDirectory , inDomains: NSSearchPathDomainMask . UserDomainMask ) docPath = urlsForDocDirectory[0] as ! NSURL //设置Web服务器 let server = Server () dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_BACKGROUND , 0), { () -> Void in server.addPostRequestHandler( Middleware .requestLogger( println )) //首页请求 server. get ( "/" ) { request, response, callback in self .createIndexHtml() let file = self .docPath. URLByAppendingPathComponent ( "index.html" ) let data = self .manager.contentsAtPath(file.path!) response.body = data callback(. Send (request, response)) } //文件请求 server. get ( "/file" ) { request, response, callback in println (request.arguments) var fileName = request.arguments[ "fileName" ] let file = self .docPath. URLByAppendingPathComponent (fileName!) let data = self .manager.contentsAtPath(file.path!) response.body = data callback(. Send (request, response)) } //启动并监听服务器 server.startListening(port: 4000, forever: true ) { result in switch result { case . Success : println ( "Up and running" ) case . Error ( let e): println ( "Server start failed \(e)" ) } } }) } //加载用户文档下的文件,并生成index.html首页 func createIndexHtml()-> Void { var content = "<html><meta charset=\"utf-8\"><body>" let contentsOfPath = manager.contentsOfDirectoryAtPath(docPath.path!, error: nil ) for child in contentsOfPath!{ content = content + "<a target=‘_blank‘ href=‘/file?fileName=\(child)‘>\(child)</a><br/>" } content = content + "</body></html>" createFile( "index.html" , fileBaseUrl: docPath, content:content) } //创建文件 func createFile(name: String ,fileBaseUrl: NSURL ,content: String ){ var error: NSErrorPointer = nil let file = fileBaseUrl. URLByAppendingPathComponent (name) println ( "文件: \(file)" ) let exist = manager.fileExistsAtPath(file.path!) if !exist { var error: NSErrorPointer = nil manager.removeItemAtPath(file.path!, error:error) } let data = content.dataUsingEncoding( NSUTF8StringEncoding , allowLossyConversion: true ) let createSuccess = manager.createFileAtPath(file.path!,contents:data, attributes: nil ) println ( "文件创建结果: \(createSuccess)" ) } override func didReceiveMemoryWarning() { super .didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } |
Swift - 做一个简单的无线U盘(手机端Http服务器搭建)
标签:
原文地址:http://www.cnblogs.com/Free-Thinker/p/4843332.html