秋楓_Lance 2019-06-05 10:47
浏览 436

Swift Alamofire调用java的图片上传http接口

1.java的http接口有两个参数,一个字符串,一个MultipartFile图片文件
接口代码如下:

@ResponseBody
@RequestMapping(value="/uploadUserImg/{userName}",method=RequestMethod.POST)
public Map<String,Object> uploadUserImg(@RequestParam("userImg") MultipartFile imgFile,@PathVariable("userName") String userName) {
}

或者
@ResponseBody
@RequestMapping(value="/uploadUserImg",method=RequestMethod.POST)
public Map<String,Object> uploadUserImg(@RequestParam("userImg") MultipartFile imgFile,@RequestParam"userName") String userName) {
}

请问怎样用Swift Alamofire编写调用方法,我在接口里打断点,下面的方法调用总是进不去接口

class FileAPI {

    static func uploadImage(userImage: Data,
                            _ reqCallBack: @escaping(Bool, JSON?, APIError?) -> Void)
    {
        let maxByte = 1024 * 1024 * 2
        if userImage.count > maxByte {
            reqCallBack(false, nil, APIError.uploadFileFail(msg: "图片大于2M,无法上传"))
            return
        }

        let url = "http://192.168.1.232:8088/httpServiceInter/uploadUserImg/admin123"
        Alamofire.upload(
            multipartFormData: { multipartFormData in
                multipartFormData.append(userImage, withName: "userImg")
        },
            to: url,
            method: .post,
            headers: ["Content-Type": "application/x-www-form-urlencoded"],
            encodingCompletion: { encodingResult in
                switch encodingResult {
                case .success(let upload, _, _):
                    upload.responseJSON { response in
                        let json = JSON(response.data!)
                        reqCallBack(true, json, nil)
                    }
                case .failure(let error):
                    reqCallBack(false, nil, APIError.unknow)
                    print(error)
                }
        }
        )
    }

错误提示:

[Request]: POST http://192.168.1.232:8088/httpServiceInter/uploadUserImg/admin123
[Response]: { URL: http://192.168.1.232:8088/httpServiceInter/uploadUserImg/admin123 } { Status Code: 400, Headers {
Connection = (
close
);
"Content-Language" = (
en
);
"Content-Length" = (
1123
);
"Content-Type" = (
"text/html;charset=utf-8"
);
Date = (
"Tue, 04 Jun 2019 05:34:50 GMT"
);
Server = (
"Apache-Coyote/1.1"
);
} }
[Data]: 1123 bytes
[Result]: FAILURE: responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))
[Timeline]: Timeline: { "Request Start Time": 581319290.237, "Initial Response Time": 581319290.455, "Request Completed Time": 581319290.524, "Serialization Completed Time": 581319290.547, "Latency": 0.218 secs, "Request Duration": 0.287 secs, "Serialization Duration": 0.023 secs, "Total Duration": 0.310 secs }
(lldb)

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器