iOS POST 文件的上传
来源:互联网 发布:阿里云搭建hadoop 编辑:程序博客网 时间:2024/05/13 19:57
#import "YYViewController.h"
#define YYEncode(str) [str dataUsingEncoding:NSUTF8StringEncoding]
@interface YYViewController ()
@end
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)upload:(NSString *)name filename:(NSString *)filename mimeType:(NSString *)mimeType data:(NSData *)data parmas:(NSDictionary *)params
{
// 文件上传
NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/YYServer/upload"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
// 设置请求体
NSMutableData *body = [NSMutableData data];
/***************文件参数***************/
// 参数开始的标志
[body appendData:YYEncode(@"--YY\r\n")];
// name : 指定参数名(必须跟服务器端保持一致)
// filename : 文件名
NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", name, filename];
[body appendData:YYEncode(disposition)];
NSString *type = [NSString stringWithFormat:@"Content-Type: %@\r\n", mimeType];
[body appendData:YYEncode(type)];
[body appendData:YYEncode(@"\r\n")];
[body appendData:data];
[body appendData:YYEncode(@"\r\n")];
/***************普通参数***************/
[params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
// 参数开始的标志
[body appendData:YYEncode(@"--YY\r\n")];
NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n", key];
[body appendData:YYEncode(disposition)];
[body appendData:YYEncode(@"\r\n")];
[body appendData:YYEncode(obj)];
[body appendData:YYEncode(@"\r\n")];
}];
/***************参数结束***************/
// YY--\r\n
[body appendData:YYEncode(@"--YY--\r\n")];
request.HTTPBody = body;
// 设置请求头
// 请求体的长度
[request setValue:[NSString stringWithFormat:@"%zd", body.length] forHTTPHeaderField:@"Content-Length"];
// 声明这个POST请求是个文件上传
[request setValue:@"multipart/form-data; boundary=YY" forHTTPHeaderField:@"Content-Type"];
// 发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (data) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"%@", dict);
} else {
NSLog(@"上传失败");
}
}];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Socket 实现断点上传
//apache-tomcat-6.0.41/conf/web.xml 查找 文件的 mimeType
// UIImage *image = [UIImage imageNamed:@"test"];
// NSData *filedata = UIImagePNGRepresentation(image);
// [self upload:@"file" filename:@"test.png" mimeType:@"image/png" data:filedata parmas:@{@"username" : @"123"}];
// 给本地文件发送一个请求
NSURL *fileurl = [[NSBundle mainBundle] URLForResource:@"itcast.txt" withExtension:nil];
NSURLRequest *request = [NSURLRequest requestWithURL:fileurl];
NSURLResponse *repsonse = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&repsonse error:nil];
// 得到mimeType
NSLog(@"%@", repsonse.MIMEType);
[self upload:@"file" filename:@"itcast.txt" mimeType:repsonse.MIMEType data:data parmas:@{
@"username" : @"999",
@"type" : @"XML"}];
}
@end
#define YYEncode(str) [str dataUsingEncoding:NSUTF8StringEncoding]
@interface YYViewController ()
@end
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)upload:(NSString *)name filename:(NSString *)filename mimeType:(NSString *)mimeType data:(NSData *)data parmas:(NSDictionary *)params
{
// 文件上传
NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/YYServer/upload"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
// 设置请求体
NSMutableData *body = [NSMutableData data];
/***************文件参数***************/
// 参数开始的标志
[body appendData:YYEncode(@"--YY\r\n")];
// name : 指定参数名(必须跟服务器端保持一致)
// filename : 文件名
NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", name, filename];
[body appendData:YYEncode(disposition)];
NSString *type = [NSString stringWithFormat:@"Content-Type: %@\r\n", mimeType];
[body appendData:YYEncode(type)];
[body appendData:YYEncode(@"\r\n")];
[body appendData:data];
[body appendData:YYEncode(@"\r\n")];
/***************普通参数***************/
[params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
// 参数开始的标志
[body appendData:YYEncode(@"--YY\r\n")];
NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n", key];
[body appendData:YYEncode(disposition)];
[body appendData:YYEncode(@"\r\n")];
[body appendData:YYEncode(obj)];
[body appendData:YYEncode(@"\r\n")];
}];
/***************参数结束***************/
// YY--\r\n
[body appendData:YYEncode(@"--YY--\r\n")];
request.HTTPBody = body;
// 设置请求头
// 请求体的长度
[request setValue:[NSString stringWithFormat:@"%zd", body.length] forHTTPHeaderField:@"Content-Length"];
// 声明这个POST请求是个文件上传
[request setValue:@"multipart/form-data; boundary=YY" forHTTPHeaderField:@"Content-Type"];
// 发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (data) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"%@", dict);
} else {
NSLog(@"上传失败");
}
}];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Socket 实现断点上传
//apache-tomcat-6.0.41/conf/web.xml 查找 文件的 mimeType
// UIImage *image = [UIImage imageNamed:@"test"];
// NSData *filedata = UIImagePNGRepresentation(image);
// [self upload:@"file" filename:@"test.png" mimeType:@"image/png" data:filedata parmas:@{@"username" : @"123"}];
// 给本地文件发送一个请求
NSURL *fileurl = [[NSBundle mainBundle] URLForResource:@"itcast.txt" withExtension:nil];
NSURLRequest *request = [NSURLRequest requestWithURL:fileurl];
NSURLResponse *repsonse = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&repsonse error:nil];
// 得到mimeType
NSLog(@"%@", repsonse.MIMEType);
[self upload:@"file" filename:@"itcast.txt" mimeType:repsonse.MIMEType data:data parmas:@{
@"username" : @"999",
@"type" : @"XML"}];
}
@end
0 0
- iOS POST 文件的上传
- iOS post上传文件
- iOS中POST上传文件的实现
- ios 文件上传, post数据
- IOS post方式上传文件
- ios通过post上传文件
- ios 文件上传, post数据
- ios 文件上传, post数据
- iOS多文件post上传
- iOS POST上传文件~笔记
- iOS 文件上传 post数据
- 关于 ios post 方式上传 文件的问题
- iOS通过HTTP POST上传文件
- iOS开发-post文件上传总结
- iOS post请求上传文件(OC)
- [绍棠] iOS post方式上传文件
- OkHttp的post上传文件
- 将一张IOS沙盒下的图片或是txt文件通过post请求上传服务器
- Windows下Tomcat配置
- classpath file user.dir
- MIUI 6 沉浸式状态栏调用方法
- Android学习(1)---最新Android Studio 1.4.1开发环境搭建与HelloWorld
- 代理模式
- iOS POST 文件的上传
- 为cocos2d-x3.2版本增加protobuffer2.5.0支持
- linux下mysql表名大小写敏感问题
- iOS学习08代码2
- 虚拟机Ubuntu图形界面进入命令行快捷键
- 开机自启动软件设置
- [转载]iTOP-4412开发板搭建最小linux系统
- 让Mysql支持Emoji表情
- 001 Two Sum [Leetcode]