2014-06-23 250 views
0

**我經歷了很多答案在stackoverflow從iPhone上傳圖像到服務器,但仍然不能提出解決方案。從iPhone上傳圖像到服務器

這是一個簡單的圖片上傳與它一起的用戶名,這裏是我的代碼看起來**

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> 

有人能讓我通過這個嗎?提前致謝。

+0

錯誤它自己回答:) –

回答

0

您不能同時發送Content-Type: image/png和字符串數據。您需要發送multipart/form-data請求。你需要設置你的服務器來處理它。

+0

@remes,感謝您編輯我的帖子清楚。我把它告訴了我的服務器端,現在看來它的工作。謝謝。 – user3766348