将数据上传至服务器的方法(post方法)

来源:互联网 发布:linux时间校准 编辑:程序博客网 时间:2024/06/14 20:24


#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (IBAction)choosePicture:(id)sender

{

    UIActionSheet *actionSheet = [[UIActionSheetalloc]initWithTitle:@"选择照片来源"delegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"从相册中选择",@"从相机拍照",nil];

    [actionSheetshowInView:self.view];

}


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

    UIImagePickerController *imagePicker = [[UIImagePickerControlleralloc]init];

    imagePicker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;

    imagePicker.allowsEditing =YES;

    imagePicker.delegate =self;

    [selfpresentViewController:imagePicker animated:YEScompletion:nil];

}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

    NSLog(@"选中了某张图片 %@",info);

    UIImage *image = [infoobjectForKey:UIImagePickerControllerEditedImage];

   _imageView.image = image;

    [picker dismissViewControllerAnimated:YEScompletion:nil];

}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

    NSLog(@"点击了取消");

    [picker dismissViewControllerAnimated:YEScompletion:nil];

}


- (IBAction)upload:(id)sender

{

    //上传数据一定要用post请求

    NSString *urlString =@"http://127.0.0.1:8080/UploadServer/NewServlet";

    NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:[NSURLURLWithString:urlString]];

    

    //UIImage转化成NSData

    NSData *data =UIImageJPEGRepresentation(_imageView.image,1);

    

//    NSString *path = [[NSBundle mainBundle] pathForResource:@"a" ofType:@"jpg"];

//    NSData *data = [[NSData alloc]initWithContentsOfFile:path];

    

    [requestsetHTTPMethod:@"post"];

    //小文件上传

    [requestsetHTTPBody:data];

    

    //大文件上传用输入流

//    NSInputStream *stream = [[NSInputStream alloc]initWithFileAtPath:[[NSBundle mainBundle] pathForResource:@"" ofType:@""]];

//    [request setHTTPBodyStream:stream];

    

    

    

    [NSURLConnectionconnectionWithRequest:request delegate:nil];

}

@end

0 0
原创粉丝点击