经过一天的蒸腾,我决定写几句话抒发一下我此时此时用了一天的时间才解决的小bug。
首先我自己觉的出现这个问题,还是自己了解的太少,其次是后台人员的原因。
言归正传。我们的需求是上传图片同时添加介绍文字。就这么一个简单的需求。最开始使用AFN上传,没有后台配合,放弃了,如果当时使用AFN可能也不会有这个问题,又差点跑题。我使用的是原生post上传图片和文字信息。android是使用的框架,唉。。。。后台使用的是DiskFileItemFactory这个框架。我也是第一次遇到这个问题很多不懂,据同学说这里需要和后台一起协定post上传的头部Content-Type。我们后台也不知道的,比我还小清新。post上传代码如下,我自己也是参考网络大牛写的,
// 多张照片上传
- (void)upLoadImageWithTID:(NSString *)user_id andMsg:(NSString *)msg andURL:(NSString *)urlStr andImageArray:(NSArray *)imageArray {
//记录Image的类型和data
NSString *imageFormat = @"Content-Type: image/png \r\n";
NSMutableData *imageArrayData = [NSMutableData data];
NSMutableArray *imageDataArr = [[NSMutableArray alloc]init];
for (int i = 0; i < imageArray.count; i ++) {
NSData *imageData = UIImagePNGRepresentation(imageArray[i]);
[imageArrayData appendData:imageData];
[imageDataArr addObject:imageData];
}
// 设置请求体
NSMutableData *body = [NSMutableData data];
/**文件参数**/
for (int i = 0; i < imageArray.count; i ++) {
[body appendData:kLSEncode(@"--LS\r\n")];
//这里注明服务器接受图片的参数(服务器指定参数名称)及服务器上保存图片的文件名
NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"File%d\"; filename=\"image%d.png\"\r\n", i+1, i+1] ;
[body appendData:kLSEncode(disposition)];
[body appendData:kLSEncode(imageFormat)];
[body appendData:kLSEncode(@"\r\n")];
NSData *da = imageDataArr[i];
[body appendData:da];
[body appendData:kLSEncode(@"\r\n")];
}
/**普通参数**/
[body appendData:kLSEncode(@"--LS\r\n")];
NSString *dispositions = @"Content-Disposition: form-data; name=\"T_id\"\r\n";
[body appendData:kLSEncode(dispositions)];
[body appendData:kLSEncode(@"\r\n")];
[body appendData:kLSEncode(user_id)]; // user_id 的值
[body appendData:kLSEncode(@"\r\n")];
[body appendData:kLSEncode(@"--LS\r\n")];
NSString *dispositionss = @"Content-Disposition: form-data; name=\"msg\"\r\n";
[body appendData:kLSEncode(dispositionss)];
[body appendData:kLSEncode(@"\r\n")];
[body appendData:kLSEncode(msg)]; // msg 的值
[body appendData:kLSEncode(@"\r\n")];
/**参数结束**/
[body appendData:kLSEncode(@"--LS--\r\n")];
// 请求地址
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
request.HTTPBody = body;
//设置请求长度
NSInteger length = [body length];
[request setValue:[NSString stringWithFormat:@"%ld", (long)length] forHTTPHeaderField:@"Content-Length"];
// 设置POST请求文件上传
[request setValue:@"multipart/form-data; boundary=LS;" forHTTPHeaderField:@"Content-Type"];
// NSDictionary *headers = @{@"Accept": @"text/html",
// @"Content-Type": @"multipart/form-data; boundary=LS;charset=utf-8;",
// @"Cache-Control": @"no-cache",
// @"Content-Length":[NSString stringWithFormat:@"%ld", (long)length]};
// [request setAllHTTPHeaderFields:headers];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"-------========-------%@", dic);
if (!dic) {
[self.hud removeFromSuperview];
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"提交失败." delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
}
if ([dic[@"ResultCode"] isEqual:@200]) {
[self.hud removeFromSuperview];
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"提交成功." delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
_isFirst = NO;
} else if([dic[@"ResultCode"] isEqual:@500]) {
[self.hud removeFromSuperview];
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"提交失败." delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
}
}];
}
后台没有写Content-Type我这里也没有, 后台说他的代码是copy来的。不知道改写啥。真是的
乱码就在传字符串到后台时是乱码。简短点说吧还在上班,之前后台得到我传的字符串直接用的getString()方法,我改成。 new String(item.getString().getBytes("ios-8859-1"), "utf-8")。解决了。
此时我就怀疑了我传的时候明明是utf-8编码,后台为啥还要从ios-8859-1编码格式转到utf-8编码。难道iOS后台传送数据是ios-8859-1编码的???,希望大神们批评指正。