iOS多线程与网络开发之小文件上传
郝萌主倾心贡献,尊重作者的劳动成果,请勿转载。
假设文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额任意,重在心意^_^
我要捐赠: 点击捐赠
Cocos2d-X源代码下载:点我传送
游戏官方下载:http://dwz.cn/RwTjl
游戏视频预览:http://dwz.cn/RzHHd
游戏开发博客:http://dwz.cn/RzJzI
游戏源代码传送:http://dwz.cn/Nret1



1 /** 取得本地文件的MIMEType */
2 - (void) getMIMEType {
3 // Socket 实现断点上传
4
5 //apache-tomcat-6.0.41/conf/web.xml 查找 文件的 mimeType
6 // UIImage *image = [UIImage imageNamed:@"test"];
7 // NSData *filedata = UIImagePNGRepresentation(image);
8 // [self upload:@"file" filename:@"test.png" mimeType:@"image/png" data:filedata parmas:@{@"username" : @"123"}];
9
10 // 给本地文件发送一个请求
11 NSURL *fileurl = [[NSBundle mainBundle] URLForResource:@"itcast.txt" withExtension:nil];
12 NSURLRequest *request = [NSURLRequest requestWithURL:fileurl];
13 NSURLResponse *repsonse = nil;
14 NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&repsonse error:nil];
15
16 // 得到mimeType
17 NSLog(@"%@", repsonse.MIMEType);
18 [self upload:@"file" filename:@"itcast.txt" mimeType:repsonse.MIMEType data:data parmas:@{@"username":@"tom", @"type":@"xml"}];
19 }



1 //
2 // ViewController.m
3 // UploadFileDemo
4 //
5 // Created by haomengzhu on 15/1/28.
6 // Copyright (c) 2015年 haomengzhu. All rights reserved.
7 //
8
9 #import "ViewController.h"
10
11 #define UTF8Encode(str) [str dataUsingEncoding:NSUTF8StringEncoding]
12
13 @interface ViewController ()
14
15 - (IBAction)upload;
16
17 @end
18
19 @implementation ViewController
20
21 - (void)viewDidLoad {
22 [super viewDidLoad];
23 // Do any additional setup after loading the view, typically from a nib.
24 }
25
26 - (void)didReceiveMemoryWarning {
27 [super didReceiveMemoryWarning];
28 // Dispose of any resources that can be recreated.
29 }
30
31 - (IBAction)upload {
32 UIImage *image = [UIImage imageNamed:@"IMG_0413"];
33 NSData *imageData = UIImagePNGRepresentation(image);
34 [self upload:@"uploadedFile" filename:@"IMG_0413.PNG" mimeType:@"image/png" data:imageData parmas:nil];
35 }
36
37 - (void)upload:(NSString *)name filename:(NSString *)filename mimeType:(NSString *)mimeType data:(NSData *)data parmas:(NSDictionary *)params
38 {
39 // 文件上传
40 NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/upload"];
41 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
42 request.HTTPMethod = @"POST";
43
44 // 设置请求体
45 NSMutableData *body = [NSMutableData data];
46
47 /***************文件參数***************/
48 // 參数開始的标志
49 [body appendData:UTF8Encode(@"--HelloVoidWorldBoundary\r\n")];
50 // name : 指定參数名(必须跟server端保持一致)
51 // filename : 文件名称
52 NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", name, filename];
53 [body appendData:UTF8Encode(disposition)];
54 NSString *type = [NSString stringWithFormat:@"Content-Type: %@\r\n", mimeType];
55 [body appendData:UTF8Encode(type)];
56
57 [body appendData:UTF8Encode(@"\r\n")];
58 [body appendData:data];
59 [body appendData:UTF8Encode(@"\r\n")];
60
61 /***************普通參数***************/
62 [params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
63 // 參数開始的标志
64 [body appendData:UTF8Encode(@"--HelloVoidWorldBoundary\r\n")];
65 NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n", key];
66 [body appendData:UTF8Encode(disposition)];
67
68 [body appendData:UTF8Encode(@"\r\n")];
69 [body appendData:UTF8Encode(obj)];
70 [body appendData:UTF8Encode(@"\r\n")];
71 }];
72
73 /***************參数结束***************/
74 // HelloVoidWorldBoundary--\r\n
75 [body appendData:UTF8Encode(@"--HelloVoidWorldBoundary--\r\n")];
76 request.HTTPBody = body;
77
78 // 设置请求头
79 // 请求体的长度
80 [request setValue:[NSString stringWithFormat:@"%zd", body.length] forHTTPHeaderField:@"Content-Length"];
81 // 声明这个POST请求是个文件上传
82 [request setValue:@"multipart/form-data; boundary=HelloVoidWorldBoundary" forHTTPHeaderField:@"Content-Type"];
83
84 // 发送请求
85 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
86 NSLog(@"開始上传~~~");
87 if (data) {
88 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
89 NSLog(@"%@", dict);
90 } else {
91 NSLog(@"上传失败");
92 }
93 }];
94 }
95
96 /** 取得本地文件的MIMEType */
97 - (void) getMIMEType {
98 // Socket 实现断点上传
99
100 //apache-tomcat-6.0.41/conf/web.xml 查找 文件的 mimeType
101 // UIImage *image = [UIImage imageNamed:@"test"];
102 // NSData *filedata = UIImagePNGRepresentation(image);
103 // [self upload:@"file" filename:@"test.png" mimeType:@"image/png" data:filedata parmas:@{@"username" : @"123"}];
104
105 // 给本地文件发送一个请求
106 NSURL *fileurl = [[NSBundle mainBundle] URLForResource:@"itcast.txt" withExtension:nil];
107 NSURLRequest *request = [NSURLRequest requestWithURL:fileurl];
108 NSURLResponse *repsonse = nil;
109 NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&repsonse error:nil];
110
111 // 得到mimeType
112 NSLog(@"%@", repsonse.MIMEType);
113 [self upload:@"file" filename:@"itcast.txt" mimeType:repsonse.MIMEType data:data parmas:@{@"username":@"tom", @"type":@"xml"}];
114 }
115
116
117 @end

郝萌主倾心贡献,尊重作者的劳动成果,请勿转载。
假设文章对您有所帮助,欢迎给作者捐赠。支持郝萌主。捐赠数额任意,重在心意^_^
我要捐赠: 点击捐赠
Cocos2d-X源代码下载:点我传送
游戏官方下载:http://dwz.cn/RwTjl
游戏视频预览:http://dwz.cn/RzHHd
游戏开发博客:http://dwz.cn/RzJzI
游戏源代码传送:http://dwz.cn/Nret1
iOS多线程与网络开发之小文件上传的更多相关文章
- [iOS 多线程 & 网络 - 2.5] - 小文件上传
A.文件上传 思路: 发送文件数据给服务器 使用post请求 必须手动设置请求头: 内容大小Content-Length & 内容类型 Content-Type 请求体:文件数据 文件上传的格 ...
- iOS开发之多文件上传
// // ViewController.m // B03-多文件上传 // // Created by 0426iOS on 15/7/1. // Copyright (c) 2015年 0 ...
- 阿里云 oss 小文件上传进度显示
对阿里云OSS上传小文件时的进度,想过两个方法:一是.通过多线程监測Inputstream剩余的字节数来计算,可是由于Inputstream在两个线程中共用,假设上传线程将Inputstream关闭, ...
- Web开发安全之文件上传安全
很长一段时间像我这种菜鸡搞一个网站第一时间反应就是找上传,找上传.借此机会把文件上传的安全问题总结一下. 首先看一下DVWA给出的Impossible级别的完整代码: <?php if( iss ...
- asp.net(c#)开发中的文件上传组件uploadify的使用方法(带进度条)
上文件传很常见,现在就文件上传利用HTML的File控件(uploadify)的,这里为大家介绍一下(uploadify)的一些使用方法.在目前Web开发中用的比较多的,可能uploadify(参考h ...
- python运维开发(二十一)----文件上传和验证码+session
内容目录: 文件上传 验证码+session 文件和图片的上传功能 HTML Form表单提交,实例展示 views 代码 HTML ajax提交 原生ajax提交,XMLHttpRequest方式上 ...
- iOS开发之AFNetworking实现数据传输和文件上传
//传输数据 1 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.r ...
- Spring boot+Vue全栈开发---Spring Boot文件上传
https://blog.csdn.net/Day_and_Night_2017/article/details/86980743 文件上传涉及到两个组件:CommonsMultipartResolv ...
- 分享一个FileUtil工具类,基本满足web开发中的文件上传,单个文件下载,多个文件下载的需求
获取该FileUtil工具类具体演示,公众号内回复fileutil20200501即可. package com.example.demo.util; import javax.servlet.htt ...
随机推荐
- web标准,可用性和可访问性
web标准,简单的说,是指html,css,JavaScript三者的分离. 网页由三部分组成:结构,表现和行为.对应的标准分为三方面: 1.结构化标准语言XHTML和XML2.表现标准语言主要包括c ...
- 【CCF】炉石传说 模拟
#include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...
- Idea插件lombok的安装和使用
C#在写一个实体类时,有属性的写法,省去了写getter和setter的麻烦. 在Java编程时,写完字段后,需要一个一个去写getter和setter方法.在使用Idea编程时,可以按住ALT+IN ...
- 向量内积(bzoj 3243)
Description 两个d 维向量A=[a1,a2,...,ad]与B=[b1,b2,...,bd]的内积为其相对应维度的权值的乘积和,即: 现有 n 个d 维向量x1,...,xn ,小喵喵想知 ...
- t4-editor使用方法 Visual T4
原文发布时间为:2011-05-17 -- 来源于本人的百度文章 [由搬家工具导入] http://visualstudiogallery.msdn.microsoft.com/40a887aa-f3 ...
- [论文]Coordination of Cluster Ensembles via Exact Methods
作者:Ioannis T. Christou, Member, IEEE IEEE TRANSACTIONS ON PATTERN ANALYSIS AND MACHINE INTELLIGENCE, ...
- Android蓝牙介绍
1. 介绍 自从Android 4.2开始,Android开始使用自己的蓝牙协议栈BlueDroid,而不是bluez BlueDroid可分为两层: - BTE: Bluetooth Embedde ...
- android init.rc命令快速对照表
注1:另外还讲述了怎样输出log: Debugging notes---------------By default, programs executed by init will drop stdo ...
- kafka术语
kafka 架构Terminology(术语) broker(代理) Kafka集群包含一个或多个服务器,这种服务器被称为broker Topic 每条发布到Kafka集群的消息都有一个类别,这个类 ...
- python 查看帮助和变量的强制转换
查看帮助 dir() 函数 查看对象都有哪些属性和方法 用法:把要查询的对象写入()括号中即可 print(dir([])) (查看列表的方法) 执行: C:\Python27\python.exe ...