AFNetworking 4.x高版本上传图片,后端采用springBoot测试
生活随笔
收集整理的這篇文章主要介紹了
AFNetworking 4.x高版本上传图片,后端采用springBoot测试
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
AFNetworking 4.x高版本上傳圖片,后端采用springBoot測試
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<AFMultipartFormData> _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圖片的/*1. name:指服務器獲取該圖片所用的“鍵”的名字2. fileName:文件在服務器上保存的名字*/[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的依賴
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.jpgMac系統沒有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
總結
以上是生活随笔為你收集整理的AFNetworking 4.x高版本上传图片,后端采用springBoot测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一个学中医女生的保养身体法
- 下一篇: Swift5以及IOS15对于二维码的使