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)