Pod加入
pod 'AFNetworking', '~> 4.0.1'
-(void)sendStatusWithImage{
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
mgr.requestSerializer= [AFHTTPRequestSerializer serializer];
mgr.responseSerializer= [AFHTTPResponseSerializer serializer];
[mgr POST:@"http://localhost:8888/upload" parameters:nil headers:nil constructingBodyWithBlock:^(id _Nonnull formData) {
NSDate *date= [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc]init];
df.dateFormat = @"yyyyMMddHHmmss";
NSString *randomFileName = [df stringFromDate:date];
NSString *newFileName = [NSString stringWithFormat:@"%@.jpg",randomFileName];
UIImage *image = [self.photosView.images firstObject];
NSData *data = UIImageJPEGRepresentation(image, 1.0);
NSLog(@"%@",data);
//NSData *data1 = UIImagePNGRepresentation(image);压缩png图片的
[formData appendPartWithFileData:data name:@"pic" fileName:newFileName mimeType:@"image/jpg"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
NSLog(@"当前进度:%lld / 总进度:%lld",uploadProgress.completedUnitCount,uploadProgress.totalUnitCount);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.label.text = @"图片上传成功";
hud.margin = 20;
hud.hm_y = -150;
hud.removeFromSuperViewonHide = YES;
[hud hideAnimated:YES afterDelay:3];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.label.text = @"发送失败";
hud.margin = 20;
hud.hm_y = -150;
hud.removeFromSuperViewonHide = YES;
[hud hideAnimated:YES afterDelay:3];
}];
}
重点是 mgr.requestSerializer= [AFHTTPRequestSerializer serializer];
mgr.responseSerializer= [AFHTTPResponseSerializer serializer];
否则AFNetworking一直报错
java的springBoot 配置,重点配置thymeleaf
否则会报错,上传的图片也不能太大,否则也会报错,除非properties加入最大的上传图片大小。
thymeleaf pom的依赖
org.springframework.boot spring-boot-starter-thymeleaf
springBoot的启动配置文件
spring.thymeleaf.mode=HTML spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.cache=false server.port=8888 # 文件路径, 注意路径末尾一定要带上/ user.file.path=/Users/lujun/Desktop/ user.file.name=springboot.jpg
Mac系统没有C盘,D盘,F盘。用/Users代替
@Controller
public class FileUploadController {
@Value("${user.file.path}")
private String filePath;
@Value("${user.file.name}")
private String fileName;
@GetMapping(value = "/index")
public ModelAndView getIndex(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
@RequestMapping("/upload")
public ModelAndView update(@RequestParam("pic") MultipartFile multipartFile) {
try {
// 保存图片
File file = new File(filePath + multipartFile.getOriginalFilename());
multipartFile.transferTo(file);
} catch (IOException e) {
e.printStackTrace();
}
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
}
仓库地址
https://gitee.com/johnson__save_admin/picture-upload-spring



