将图像从iPhone上传到服务器

时间:2014-06-23 06:45:11

标签: ios iphone objective-c xcode

**我在stackoverflow中经历了很多答案,用于将图像从iPhone上传到服务器,但仍然无法提供解决方案。

这是一个带有UserID的简单图片上传,这里是我的代码看起来的样子**

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;

request.timeoutInterval = 60;

[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"image/png" forHTTPHeaderField:@"Content-Type"];    
[request setValue:@"07ee7054-010b-44da-a14f-02e3e66800b7" forHTTPHeaderField:@"userId"];
[request imageData];

conn = [[NSURLConnection alloc]
        initWithRequest:request
        delegate:self startImmediately:NO];

[conn scheduleInRunLoop:[NSRunLoop mainRunLoop]
                forMode:NSDefaultRunLoopMode];
[conn start];

if (conn)
{
    receivedData = nil;
    receivedData = [NSMutableData data] ;
    [receivedData setLength:0];
}

这就是我服务器端的外观,

@RequestMapping(value = "/uploadphoto", method = RequestMethod.POST, headers="Accept = */*")
public Map<String,Object> uploadPhoto(InputStream in,@RequestBody String JSONContentofPOST)
{
    return UploadPhoto(in,JSONContentofPOST);
} 

我得到的回复

<html>
   <head>
      <title>Apache Tomcat/7.0.53 - Error report</title>
      <style>
         <!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}-->
      </style>
   </head>
   <body>
      <h1>HTTP Status 400 - Required String parameter 'userId' is not present</h1>
      <HR size="1" noshade="noshade">
      <p><b>type</b> Status report</p>
      <p><b>message</b> <u>Required String parameter 'userId' is not present</u></p>
      <p><b>description</b> <u>The request sent by the client was syntactically incorrect.</u></p>

有人可以帮我解决一下吗?提前致谢。

1 个答案:

答案 0 :(得分:0)

您无法同时发送Content-Type: image/png AND字符串数据。您需要发送multipart/form-data请求。您需要设置服务器来处理它。