[英]cURL post request php to spring
我必須使用 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
注釋 spring 控制器方法會發生什么?
@CrossOrigin
@RequestMapping(value = "/upload/teacher" ,method = RequestMethod.POST)
public HttpStatus uploadFiles(@RequestParam("files") MultipartFile[] inputFiles,
@RequestParam("assignmentID") String assignmentID) throws IOException { ... }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.