NSURLConnection上傳

//上傳用post請求文件較大

//上傳文件

- (void)upload {

//設(shè)置路徑

NSURL*url = [NSURL URLWithString:@"http://localhost:8080/UpLoad/NewServlet"];

//請求對象

NSMutableURLRequest *postRequest = [NSMutableURLRequestrequestWithURL:url];

//請求方式

[postRequest setHTTPMethod:@"POST"];

//上傳數(shù)據(jù)要用表單提交數(shù)據(jù)multipart/form-data:新的編碼格式,可以提高數(shù)據(jù)長傳的效率

[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;

}

//轉(zhuǎn)化為二進制流

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系統(tǒng)獲取圖片和視屏的接口

UIImagePickerController *picker = [[UIImagePickerController alloc] init];

//設(shè)置數(shù)據(jù)來源(三種:圖庫相冊相機)

picker.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;

//判斷獲取本機的數(shù)據(jù)來源

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

NSLog(@"圖庫可用");

}

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

NSLog(@"相機可用");

}

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {

NSLog(@"相冊可用");

}

//設(shè)置允許編輯開可以在代理方法中對圖片進行編輯

picker.allowsEditing=YES;

//連個代理協(xié)議

picker.delegate = self;

//模態(tài)顯示

[self presentViewController:picker animated:YES completion:nil];

}

#pragma mark -- UIImagePickerControllerDelegate,UINavigationControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {//選照片時調(diào)用

//別名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"];

//上傳數(shù)據(jù)要用表單提交數(shù)據(jù)multipart/form-data:新的編碼格式,可以提高數(shù)據(jù)長傳的效率

[postRequest addValue:@"multipart/form-data" forHTTPHeaderField:@"Content-type"];

//壓縮圖片的圖片的方法

//壓縮性能較差較大

NSData *data =UIImagePNGRepresentation(self.imageView.image);

//壓縮系數(shù)1 2可調(diào),較小

NSData *data1 =UIImageJPEGRepresentation(self.imageView.image, 2);

#pragma unused (data1)

//設(shè)置對象請求體

[postRequest setHTTPBody:data];

//發(fā)送請求

[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按鈕時調(diào)用

[self dismissViewControllerAnimated:YES completion:nil];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容