公司這邊上傳圖片,伺服器端人員說是已圖片流的方式上傳圖片,我嘗試瞭以下幾種方式都不行:
[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()];
應用程式/八位元組流