zoukankan      html  css  js  c++  java
  • 网络编程练习 -- 文件上传

    LWTViewController.m

    //
    //  LWTViewController.m
    //  网络编程练习 -- 文件上传
    //
    //  Created by apple on 14-6-30.
    //  Copyright (c) 2014年 lwt. All rights reserved.
    //
    
    #import "LWTViewController.h"
    #define KStringToData(string) [string dataUsingEncoding:NSUTF8StringEncoding]
    
    @interface LWTViewController ()
    
    @end
    
    @implementation LWTViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        // 获取文件类型和data
        NSURL *imageUrl = [[NSBundle mainBundle] URLForResource:@"test.png" withExtension:nil];
        NSURLRequest *imageRequest = [NSURLRequest  requestWithURL:imageUrl];
        NSURLResponse *response = nil;
        NSData *data = [NSURLConnection sendSynchronousRequest:imageRequest returningResponse:&response error:nil];
        NSString *mimeType = response.MIMEType;
        
        [self upload:@"file" fileName:@"test.png" mimeType:mimeType data:data params:@{
                                                                                       @"username" : @"999",
                                                                                       @"type" : @"XML"
                                                                                       }];
        
        
        
    }
    
    - (void)upload: (NSString *)name fileName:(NSString *)fileName mimeType:(NSString *)mimeType data:(NSData *)data params :(NSDictionary *)params
    {
        // 文件上传
        NSURL *url = [NSURL URLWithString:[@"http://192.168.1.24:8080/MJServer/upload" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        request.HTTPMethod = @"POST";
        
        
        
        // 设置请求体
        NSMutableData *body = [NSMutableData data];
        
        /***************文件参数***************/
        // 参数开始的标志
        [body appendData:KStringToData(@"--upload
    ")];
        // name : 指定参数名(必须跟服务器端保持一致)
        // filename : 文件名
        NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name="%@"; filename="%@"
    ", name, fileName];
        [body appendData:KStringToData(disposition)];
        
        NSString *str = [NSString stringWithFormat:@"Content-Type: %@
    ", mimeType];
        [body appendData:KStringToData(str)];
        
        [body appendData:KStringToData(@"
    ")];
        [body appendData:data];
        [body appendData:KStringToData(@"
    ")];
        
        /***************普通参数***************/
        [body appendData:KStringToData(@"--upload
    ")];
        [params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            [body appendData:KStringToData(@"--upload
    ")];
            NSString *dispos = [NSString stringWithFormat:@"Content-Disposition: form-data; name="%@"
    ", key];
            [body appendData:KStringToData(dispos)];
            [body appendData:KStringToData(@"
    ")];
            [body appendData:KStringToData(obj)];
            [body appendData:KStringToData(@"
    ")];
        }];
        
        [body appendData:KStringToData(@"--heima--
    ")];
        request.HTTPBody = body;
        
        // 设置请求头
        // 请求体的长度
        [request setValue:[NSString stringWithFormat:@"%zd", body.length] forHTTPHeaderField:@"Content-Length"];
        // 声明这个POST请求是个文件上传
        [request setValue:@"multipart/form-data; boundary=upload" forHTTPHeaderField:@"Content-Type"];
        
        // 发送请求
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            if (data) {
                NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
                NSLog(@"%@", dict);
            } else {
                NSLog(@"上传失败");
            }
        }];
    }
    @end
    View Code
  • 相关阅读:
    CF932E Team Work
    BZOJ 4480 [JSOI2013] 快乐的jyy
    CF285E Positions in Permutations
    P4312 [COCI 2009] OTOCI / 极地旅行社
    P3327 [SDOI2015]约数个数和
    P3649 [APIO2014]回文串
    P3181 [HAOI2016]找相同字符
    P3346 [ZJOI2015]诸神眷顾的幻想乡
    P4248 [AHOI2013]差异
    P4512 【模板】多项式除法
  • 原文地址:https://www.cnblogs.com/wentianblog/p/3820771.html
Copyright © 2011-2022 走看看