//上传用post请求文件较大
//上传文件
- (void)upload {
//设置路径
NSURL*url = [NSURL URLWithString:@"http://localhost:8080/UpLoad/NewServlet"];
//请求对象
NSMutableURLRequest *postRequest = [NSMutableURLRequestrequestWithURL:url];
//请求方式
[postRequest setHTTPMethod:@"POST"];
//上传数据要用表单提交数据multipart/form-data:新的编码格式,可以提高数据长传的效率
[postRequest addValue:@"multipart/form-data" forHTTPHeaderField:@"Content-type"];
//上传工程文件
//NSString *filePath = [[NSBundle mainBundle] pathForResource:@"8" ofType:@"jpg"];
//上传本地文件
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/yinyue.mp3"];
NSLog(@"---->%@",filePath);
if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
return;
}
//转化为二进制流
NSData *data = [NSData dataWithContentsOfFile:filePath];
[postRequest setHTTPBody:data];
[NSURLConnection sendAsynchronousRequest:postRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *_Nullableresponse,NSData *_Nullabledata,NSErro r*_NullableconnectionError) {
NSLog(@"---data ---%@",data);
NSLog(@"----->%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
[self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:[UIImage imageWithContentsOfFile:filePath] waitUntilDone:NO];
}];
}
//UIImagePickerController上传图片
- (void)uploadAlbum {
//UIImagePickerController系统获取图片和视屏的接口
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//设置数据来源(三种:图库相册相机)
picker.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;
//判断获取本机的数据来源
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
NSLog(@"图库可用");
}
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSLog(@"相机可用");
}
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
NSLog(@"相册可用");
}
//设置允许编辑开可以在代理方法中对图片进行编辑
picker.allowsEditing=YES;
//连个代理协议
picker.delegate = self;
//模态显示
[self presentViewController:picker animated:YES completion:nil];
}
#pragma mark -- UIImagePickerControllerDelegate,UINavigationControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {//选照片时调用
//别名didFinishPickingMediaWithInfo
if(picker.allowsEditing) {
//UIImagePickerControllerEditedImage
//UIImagePickerControllerOriginalImage
//编辑图片裁剪后的图片
self.imageView.image= [info objectForKey:UIImagePickerControllerEditedImage];
}else{
//选择原始
self.imageView.image= [info objectForKey:UIImagePickerControllerOriginalImage];
}
//路径
NSURL*url = [NSURL URLWithString:@"http://localhost:8080/UpLoad/NewServlet"];
//对象
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:url];
//方式
[postRequestset HTTPMethod:@"POST"];
//上传数据要用表单提交数据multipart/form-data:新的编码格式,可以提高数据长传的效率
[postRequest addValue:@"multipart/form-data" forHTTPHeaderField:@"Content-type"];
//压缩图片的图片的方法
//压缩性能较差较大
NSData *data =UIImagePNGRepresentation(self.imageView.image);
//压缩系数1 2可调,较小
NSData *data1 =UIImageJPEGRepresentation(self.imageView.image, 2);
#pragma unused (data1)
//设置对象请求体
[postRequest setHTTPBody:data];
//发送请求
[NSURLConnection sendAsynchronousRequest:postRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *_Nullableresponse,NSData *_Nullabledata,NSError *_NullableconnectionError) {
NSLog(@"----->%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
//cancel按钮时调用
[self dismissViewControllerAnimated:YES completion:nil];
}