objective-c - iOS以串流的形式上傳圖片
怪我咯
怪我咯 2017-06-06 09:52:38
0
1
854

公司這邊上傳圖片,伺服器端人員說是已圖片流的方式上傳圖片,我嘗試瞭以下幾種方式都不行:

[manager POST:@"http://xxxxxxxx:8094/interface_normal/editorFile/uploadImages.do" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    NSData* imageData = UIImageJPEGRepresentation(images[i], 0.5);
    [formData appendPartWithFileData:imageData name:@"file" fileName:@"tupian.png" mimeType:@"image/png"];
} success:^(NSURLSessionDataTask *task, id responseObject) {
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
    
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    
}];

還有如下的方式:

NSData *data = UIImageJPEGRepresentation(image, 0.5f);
NSString *uploadUrl = [NSString stringWithFormat:@"%@editorFile/uploadImages.do", BASE_URL];
NSInputStream *imageStream = [[NSInputStream alloc] initWithData:data];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:uploadUrl]];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%ld", [data length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/octet-stream" forHTTPHeaderField:@"Content-Type"];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.inputStream = imageStream;

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    completionBlock(responseObject, nil);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    completionBlock(operation.responseObject,error);
}];

[operation start];

還是上傳失敗,請大神幫忙解答

安卓端上傳時可以的,使用Charles抓包安卓上傳圖片的介面如下:

#iOS這邊抓包的結果如下,嘗試過很過方式,伺服器提示上傳失敗

#另外伺服器端告訴我,他是這樣取得輸入流的,但是他取得我的輸入流是nil

InputStream is = request.getInputStream();
            
            DataInputStream input = new DataInputStream(is); 
            byte[] buf = new byte[2048];
            byte[] tb = new byte[input.readInt()];


怪我咯
怪我咯

走同样的路,发现不同的人生

全部回覆(1)
左手右手慢动作

應用程式/八位元組流

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!