【发布时间】:2020-04-26 01:39:24
【问题描述】:
我必须使用 PHP 和 cURL 将文件数组发送到 spring 服务器。
这是弹簧控制器:
@RequestMapping(value = "/upload/teacher" ,method = RequestMethod.POST)
public HttpStatus uploadFiles(@RequestParam("files") MultipartFile[] inputFiles,
@RequestParam("assignmentID") String assignmentID) throws IOException { ... }
PHP 卷曲:
<?php
$filenames = array(pathfile1, pathfile2);
$postparameters = array(
'files' => array(
new CURLFile($filenames[0], "text/plain", pathinfo($filenames[0], PATHINFO_BASENAME)),
new CURLFile($filenames[1], "text/plain", pathinfo($filenames[1], PATHINFO_BASENAME))
),
'assignmentID' => "0008"
);
print_r($postparameters);
//init curl
$url = "http://localhost:8090/upload/teacher";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postparameters,
CURLOPT_HTTPHEADER => array(
"Content-Type: multipart/form-data"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
但使用此代码,后端不会收到任何内容。
如果我从终端发送 curl 请求,它可以工作:
curl -F files=@"pathfile1","pathfile2" -F assignmentID="1" http://localhost:8090/upload/teacher
它也适用于邮递员:
https://i.stack.imgur.com/Mo3yg.png]1
谁能告诉我为什么当我从 php 运行请求时后端没有收到文件?
【问题讨论】:
-
你添加了吗:@CrossOrigin(origins = "http:// yourphp host) baeldung.com/spring-cors