搜索

首页  >  问答  >  正文

objective-c - iOS以流的形式上传图片

公司这边上传图片,服务器端人员说是已图片流的方式上传图片,我尝试了如下几种方式都不行:

[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()];


怪我咯怪我咯2730 天前870

全部回复(1)我来回复

  • ringa_lee

    ringa_lee2017-06-06 09:54:38

    应用程序/八位字节流

    回复
    0
  • 取消回复