先说几个异常:
Required request part 'file' is not present
Required request part param is not present
Spring File Upload - 'Required request part is not present'
如果你使用php,对接api端的上传文件接口,包括java接口等,出现以上问题,只需要处理相应的参数即可。
header头,可以不写Content-Type:multiple/form-data
上传的文件地址,请务必要进行CURLFile处理。
代码贴下做参考:
$resource = $_SERVER['DOCUMENT_ROOT'].'/files/aaa.jpg';
$headers = ["header"=>array('Authorization:'.$ysyToken)];
$ysyData['file'] = new \CURLFile($resource);
$result = CurlToolkit::requestYsyApi('POST','www.baidu.com',$ysyData,$headers);
curl函数也贴出来做参考
public static function requestYsyApi($method, $url, $params = array(), $conditions = array())
{
$conditions['userAgent'] = isset($conditions['userAgent']) ? $conditions['userAgent'] : '';
$conditions['connectTimeout'] = isset($conditions['connectTimeout']) ? $conditions['connectTimeout'] : 10;
$conditions['timeout'] = isset($conditions['timeout']) ? $conditions['timeout'] : 10;
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERAGENT, $conditions['userAgent']);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $conditions['connectTimeout']);
curl_setopt($curl, CURLOPT_TIMEOUT, $conditions['timeout']);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if(isset($conditions['header']) && $conditions['header']){
curl_setopt($curl, CURLOPT_HEADER, 0);
}else{
curl_setopt($curl, CURLOPT_HEADER, 1);
}
if ($method == 'POST') {
curl_setopt($curl, CURLOPT_POST, 1);
//TODO
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
} elseif ($method == 'PUT') {
curl_setopt($curl,CURLOPT_HTTPHEADER,array("X-HTTP-Method-Override: $method"));//设置HTTP头信息
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
} elseif ($method == 'DELETE') {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
} elseif ($method == 'PATCH') {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
} else {
if (!empty($params)) {
$url = $url.(strpos($url, '?') ? '&' : '?').http_build_query($params);
}
}
if(isset($conditions['header']) && $conditions['header']){
curl_setopt($curl, CURLOPT_HTTPHEADER, $conditions['header']);
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
$response = curl_exec($curl);
$curlinfo = curl_getinfo($curl);
curl_close($curl);
if (empty($curlinfo['namelookup_time'])) {
return array();
}
if (isset($conditions['contentType']) && $conditions['contentType'] == 'plain') {
return $response;
}
$body = json_decode($response, true);
return $body;
}