【问题标题】:Uploading Image from iPhone to Server将图像从 iPhone 上传到服务器
【发布时间】:2014-06-23 06:45:10
【问题描述】:

**我在 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>

有人可以帮我解决这个问题吗?提前致谢。

【问题讨论】:

  • 错误它自己回答:)

标签: ios iphone objective-c xcode


【解决方案1】:

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

【讨论】:

  • @remes,感谢您清晰地编辑我的帖子。我把它告诉了我的服务器端,它现在似乎可以工作了。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-28
相关资源
最近更新 更多