upload怎么封装 ios
发布网友
发布时间:2022-06-09 17:37
我来回答
共1个回答
热心网友
时间:2024-11-29 05:48
1.设置请求头
* 目的:告诉服务器请求体里面的内容并非普通的参数,而是包含了文件参数
[request setValue:@"multipart/form-data; boundary=maljob" forHTTPHeaderField:@"Content-Type"];1
2.设置请求体
* 作用:存放参数(文件参数和非文件参数)
1> 非文件参数
[body appendData:MalJobEncode(@"--maljob\r\n")];
[body appendData:MalJobEncode(@"Content-Disposition: form-data; name=\"username\"\r\n")];12
[body appendData:MalJobEncode(@"\r\n")];
[body appendData:MalJobEncode(@"张三")];
[body appendData:MalJobEncode(@"\r\n")];123
2> 文件参数
[body appendData:MalJobEncode(@"--heima\r\n")];
[body appendData:MalJobEncode(@"Content-Disposition: form-data; name=\"file\"; filename=\"test123.png\"\r\n")];
[body appendData:MalJobEncode(@"Content-Type: image/png\r\n")];123
[body appendData:MalJobEncode(@"\r\n")];
[body appendData:imageData];
[body appendData:MalJobEncode(@"\r\n")];123
3> 结束标记 :参数结束的标记
[body appendData:MalJobEncode(@"--maljob--\r\n")];1
文件的MIMEType
1.百度搜索
2.查找服务器下面的某个xml文件
apache-tomcat-6.0.41\conf\web.xml1
3.加载文件时通过Reponse获得
- (NSString *)MIMEType:(NSURL *)url
{
// 1.创建一个请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 2.发送请求(返回响应)
NSURLResponse *response = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
// 3.获得MIMEType
return response.MIMEType;
}12345678910
文件上传封装工具类
@interface UploadTool : NSObject
/**
* 根据文件路径获取文件的MIMEType
*
* @param url 文件路径
*
* @return 文件MIMEType
*/
- (NSString *)MIMEType:(NSURL *)url;
/**
* 根据文件名、MIMEType、二进制文件、其他的参数上传文件
*
* @param filename 文件名
* @param mimeType MIMEType
* @param fileData 二进制文件
* @param params 非文件的其他详细参数
*/
- (void)upload:(NSString *)filename mimeType:(NSString *)mimeType fileData:(NSData *)fileData params:(NSDictionary *)params;
@end123456789101112131415161718192021222324
#define MJFileBoundary @"MalJob"
#define MJNewLine @"\r\n"
#define MJEncode(str) [str dataUsingEncoding:NSUTF8StringEncoding]
#import "UploadTool.h"
@implementation UploadTool
- (NSString *)MIMEType:(NSURL *)url
{
// 1.创建一个请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 2.发送请求(返回响应)
NSURLResponse *response = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
// 3.获得MIMEType
return response.MIMEType;
}
- (void)upload:(NSString *)filename mimeType:(NSString *)mimeType fileData:(NSData *)fileData params:(NSDictionary *)params
{
// 1.请求路径
NSURL *url = [NSURL URLWithString:@"http://218.83.161.124:8080/job/upload"];
// 2.创建一个POST请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
// 3.设置请求体
NSMutableData *body = [NSMutableData data];
// 3.1.文件参数
[body appendData:MJEncode(@"--")];
[body appendData:MJEncode(MJFileBoundary)];
[body appendData:MJEncode(MJNewLine)];
NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"", filename];
[body appendData:MJEncode(disposition)];
[body appendData:MJEncode(MJNewLine)];
NSString *type = [NSString stringWithFormat:@"Content-Type: %@", mimeType];
[body appendData:MJEncode(type)];
[body appendData:MJEncode(MJNewLine)];
[body appendData:MJEncode(MJNewLine)];
[body appendData:fileData];
[body appendData:MJEncode(MJNewLine)];
// 3.2.非文件参数
[params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[body appendData:MJEncode(@"--")];
[body appendData:MJEncode(MJFileBoundary)];
[body appendData:MJEncode(MJNewLine)];
NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"", key];
[body appendData:MJEncode(disposition)];
[body appendData:MJEncode(MJNewLine)];
[body appendData:MJEncode(MJNewLine)];
[body appendData:MJEncode([obj description])];
[body appendData:MJEncode(MJNewLine)];
}];
// 3.3.结束标记
[body appendData:MJEncode(@"--")];
[body appendData:MJEncode(MJFileBoundary)];
[body appendData:MJEncode(@"--")];
[body appendData:MJEncode(MJNewLine)];
request.HTTPBody = body;
// 4.设置请求头(告诉服务器这次传给你的是文件数据,告诉服务器现在发送的是一个文件上传请求)
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", MJFileBoundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
// 5.发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
MalJobLog(@"%@", dict);
}];
}
- (void)upload
{
// 非文件的其他详细参数
NSDictionary *params = @{
@"" : @"",
@"" : @"",
};
NSURL *url = [[NSBundle mainBundle] URLForResource:@"filename" withExtension:@"txt"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *MIMEType = [self MIMEType:url];
[self upload:@"filename.txt" mimeType:MIMEType fileData:data params:params];
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102