• 上传图片的实现


    - (IBAction)uploadButton:(id)sender {

    UIImage *image = [UIImage imageNamed:@"1.jpg"];

    NSData *imageData = UIImageJPEGRepresentation(image,

    0.5);

    NSString *urlString =@"http://192.168.1.113:8090/

    NSMutableURLRequest *request = [[NSMutableURLRequest

    alloc] init] ;

    [request setURL:[NSURL URLWithString:urlString]];

    [request setHTTPMethod:@"POST"];

    NSString *boundary = [NSString

    stringWithString:@"---------------------------14737809831

    466499882746641449"];

    NSString *contentType = [NSString

    stringWithFormat:@"multipart/form-data;boundary=

    %@",boundary];

    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];

    NSMutableData *body = [NSMutableData data];

    [body appendData:[[NSString stringWithFormat:@" --

    %@ ",boundary]

    dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[NSString

    stringWithString:@"Content-Disposition:form-data; name=

    "userfile"; filename="2.png" "]

    dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[NSString

    stringWithString:@"Content-Type: application/octet-stream

    "] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[NSData dataWithData:imageData]];

    [body appendData:[[NSString stringWithFormat:@" --

    %@-- ",boundary]

    dataUsingEncoding:NSUTF8StringEncoding]];

    [request setHTTPBody:body];

    NSData *returnData = [NSURLConnection

    sendSynchronousRequest:request returningResponse:nilerror:nil];

    NSString *returnString = [[NSString alloc]

    initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(@"3-%@",returnString);

    UIImage *myImage = [UIImage imageWithData:returnData];

    [imageView setImage:myImage];

    [self.view addSubview:imageView];

  • 相关阅读:
    主线程和子线程的区别
    正则表达式 之 常用实例
    Asp.Net 之 未能加载文件或程序集 system.web.extensions 解决方法
    .Net 与 Java 的服务接口相互调用
    C# 之 SqlConnection 类
    ADO.Net 之 数据库连接池(一)
    C# 之 DataReader 和 DataSet 的区别
    Asp.Net 之 缓存机制
    IIS 之 失败请求跟踪规则
    IIS 之 HTTP错误信息提示
  • 原文地址:https://www.cnblogs.com/CJH5209/p/6072147.html
Copyright © 2020-2023  润新知