图片上传服务器(formData形式)
网上有许多图片上传服务器的方法,但是我通过各种尝试之后发现只有这种能够和我们后台的开发人员相沟通,在此做点记录。
注意:不管是多图上传还是单图上传,都需要用到AFNetWorking这个三方库(具体怎么用cocoapods导入我就不说了,百度一下就有),然后导入头文件
#import <AFNetworking/AFNetworking.h>
1.单图上传
(1)服务器接口文档:
(2)服务器相关代码:
后台基本实现思路:使用java的ftpclient,上传文件到服务器,然后nginx配置静态资源访问就行了
public static String uploadFile(FtpUser user, String remoteSubPath, String remoteFileName, InputStream input)
throws FileNotFoundException {
String result = null;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(user.getHost(), user.getPort());// 连接FTP服务器
// 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
ftp.login(user.getUserName(), user.getPassWord());// 登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
// 切换到上传目录
if (!ftp.changeWorkingDirectory(user.getRemoteBasePath() + remoteSubPath)) {
// 如果目录不存在,创建目录
String[] dirs = remoteSubPath.split("/");
String tempPath = user.getRemoteBasePath();
for (String dir : dirs) {
if (null == dir || "".equals(dir))
continue;
tempPath += "/" + dir;
if (!ftp.changeWorkingDirectory(tempPath)) {
if (!ftp.makeDirectory(tempPath)) {
return result;
} else {
ftp.changeWorkingDirectory(tempPath);
}
}
}
}
// 设置上传文件的类型为二进制类型
ftp.setFileType(FTP.BINARY_FILE_TYPE);
// 设置被动模式,默认为主动模式
ftp.enterLocalPassiveMode();
// 上传文件
String newName = remoteFileName.replaceAll("^([\\s\\S]+)(\\.\\w+)", // 重命名该文件,以配合前端的缓存策略
"$1" + DateTime.now().toString("yyyyMMddHHmmssSSS") + "$2");
if (!ftp.storeFile(newName, input))
return result;
input.close();
ftp.logout();
result = user.getHost() + "/" + "images/" + remoteSubPath + "/" + newName;
logger.info("图片上传路径:" + result);
} catch (IOException e) {
logger.error("文件上传失败", e);
return null;
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
logger.error("ftp关闭失败", ioe);
return null;
}
}
}
return result;
}
(3)iOS端相关代码:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html",@"image/jpeg",@"image/png",@"application/octet-stream",@"text/json",nil];
//上传超时设置
manager.requestSerializer.timeoutInterval = 10.0;
//重点注意:para中的data的key值要和下面文件流的name一致,不然服务器会收到字符串而不是文件。
NSData *data = UIImagePNGRepresentation(image);
NSDictionary *para = @{@"img":data,@"jSessionId":ID};
[manager POST:@"这儿填你们后台给的完整的上传图片的url" parameters:para constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
//上传的参数(上传图片,以文件流的格式)
[formData appendPartWithFileData:data name:@"img" fileName:@"这儿名字可以随便取" mimeType:@"image/png"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSInteger errCode = [responseObject[@"errorCode"] integerValue];
if (errCode == 0) {
NSLog(@"上传成功%@",responseObject);
}else{
NSLog(@"错误内容%@",responseObject[@"errMessage"]);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"上传失败%@",error.description);
}];
2、多图上传服务器
(1)服务器接口文档:
(2)服务器相关代码:
多图上传就是循环调用单图上传方法
public static String uploadImg(String jSessionId, List<MultipartFile> img) throws Exception {
StringBuffer sb = new StringBuffer();
for (MultipartFile file : img) {
String path = uploadImg(jSessionId + DateTime.now().getMillisOfSecond(), file);
if (path == null)
return null;
sb.append(path + ",");
}
return sb.toString().replaceAll("^([\\s\\S]+),", "$1");
}
(3)iOS端相关代码:
//这里的imageArr是我前面代码里选择出来的图片,放在该数组里的
//imageDataArr同上,这里只贴出了上传步骤,选图和压图,网上都有很多三方
NSString *urlStr = @"这儿写后台给你的完整url";
for (UIImage *image in self.imageArr) {
NSData * imageData = UIImageJPEGRepresentation(image, 0.5);
[self.imageDataArr addObject:imageData];
}
NSDictionary *para = @{@"img":self.imageDataArr[0],@"jSessionId":ID};
AFHTTPSessionManager *imageManager = [AFHTTPSessionManager manager];
imageManager.responseSerializer = [AFHTTPResponseSerializer serializer];
[imageManager POST:urlStr parameters:para constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
// 上传 多张图片
for(NSInteger i = 0; i < self.imageDataArr.count; i++) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
//将多张图的二进制文件拼在一起(name后面的value一定要跟后台给的接口文档的key一致)
[formData appendPartWithFileData:self.imageDataArr[i] name:@"img" fileName:fileName mimeType:@"image/jpeg"];
}
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//将服务器返回的data转为字典
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"服务器返回内容:%@",result);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"上传失败错误:%@", error.localizedDescription);
}];