如何将字节数组发送到服务器

时间:2014-05-19 19:23:18

标签: ios

我是ios的新手,我是一个Android开发人员,我有代码是我将图像转换为字节数组并发送到服务器。这是我在java中的代码,

String sResponse = "";
                String url = webpath+"user_upload.php?";
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                MultipartEntity entity = new MultipartEntity();
                entity.addPart("first_name", new StringBody(f_name));
                entity.addPart("last_name", new StringBody(l_name));
                entity.addPart("user_image_name", new ByteArrayBody(
                        image_byte_array, "image/png", ".png"));

                httpPost.setEntity(entity);
                HttpResponse response = httpClient.execute(httpPost);

                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(
                                response.getEntity().getContent(), "UTF-8"));

任何人都可以帮我在ios中实现这个,试过很多代码他们不适合我..这里是ios中的代码

NSData *bytes= UIImageJPEGRepresentation(profilePicImageView.image,0.0);
        NSString *recordPostLength = [NSString stringWithFormat:@"%d", [bytes length]];
        NSMutableString *urlstr = [NSMutableString stringWithFormat:@"%@", WEB_URL];
        [urlstr appendFormat:@"first_name=%@", firstName];
        [urlstr appendFormat:@"&last_name=%@",lastName];
        [urlstr appendFormat:@"&user_image_name=%@",recordPostLength];
        NSMutableURLRequest *recordRequest = [[NSMutableURLRequest alloc] init] ;
        [recordRequest setURL:[NSURL URLWithString:urlstr]];
        [recordRequest setHTTPMethod:@"POST"];
        [recordRequest setValue:recordPostLength forHTTPHeaderField:@"Content-Length"];
        [recordRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [recordRequest setHTTPBody:bytes];
        NSURLResponse *recordResponse;
        NSError *recordError;
        NSData *recordResponseData = [NSURLConnection sendSynchronousRequest:recordRequest returningResponse:&recordResponse error:&recordError];
        NSString *recordResp = [[NSString alloc]initWithData:recordResponseData encoding:NSUTF8StringEncoding];
        NSLog(@"recordResp:%@", recordResp);

0 个答案:

没有答案