我正在使用tomcat和apache(PHP,苗条框架)来运行一些API。对于某些API,PHP api将使用curl请求调用java api,并将响应发送回用户。 JAVA中有一个API,可将pdf附件作为可下载文件返回,其中我将Content-Type设置为“ Content-Type:APPLICATION / OCTET-STREAM”。在php api中,当将此响应返回给用户时,我将Content-Type设置为相同,但是当用户请求时,content-type为text / html。因此,下载无法在移动浏览器中进行(通过移动chrome测试),但可以在桌面浏览器中进行。当我直接请求Java API时,我将获得正确的内容类型并可以下载。
我已经尝试在php中设置content-type,但是它似乎没有从text / html更改。我不确定自己在做什么错,还有什么地方需要改变。
这是代码。
JAVA响应文件下载:
Bool
这是PHP api代码
//send the file in response to download
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", "attachment;filename="+nameOfFile+"");
FileInputStream fileInputStream = new FileInputStream(filePath);
int i=0;
while((i = fileInputStream.read()) != -1){
out.write(i);
}
inputStream.close();
fileInputStream.close();
out.close();
在上面的代码中,我在返回数据之前设置了内容类型标头,但是在响应中,我仍然看到 function DownloadReport($request, $response, $args)
{
$path = $request->getUri()->getPath();
$path = str_ireplace("v1/", "", $path);
$query = $request->getParsedBody();
if(is_null($query)){
$query = $request->getQueryParams();
}
$url = Constants::$BASE_URL . $path;
$result = Constants::sendCurlRequestForFileStream($url, "post", $query);
if ($result['status'] == "200") {
$fileName = "Lead_".strtotime(date('Y-m-d H:i:s')).".pdf";
header('Content-disposition: attachment;filename='.$fileName);
header("Content-type: APPLICATION/OCTET-STREAM");
return $result['data'];
} else {
return $response->withJson($result['data'], Constants::$UNAUTHORIZED_CODE);
}
}
。
这是Curl请求代码
Content-Type: text/html; charset=UTF-8
请让我知道是否需要更多信息来解决此问题。