问题描述
我尝试使用 axios 发送前端生成的 PDF,以保存在后端。
我似乎无法弄清楚所有类型。
axios.post(`http://localhost:8080/pdfs`,
data,
{
responseType: 'arraybuffer',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/pdf'
}
})
Java返回:
@POST
@Path("/pdfs")
public void test(final File req,
final @Context SecurityContext securityContext) throws IOException, SQLException {
pdfService.init(req);
}
所以很简单的东西。 PDF 在其他方面也很好,但是像 ? 和 ? 这样的字符被破坏了,甚至有些空格被打印为“?”
这似乎发生在 pdf 被发送到后端的路上,因为其他按钮只是从前端下载相同的 PDF(没有连接到后端)是 100% 好的。
我应该使用 responseType: Blob,还是尝试调整 java 端?
当在控制台中打印时,到达时后端侧的数据看起来像这样(仍然包含所有特殊字符):
%PDF-1.3
%???à
3 0 obj
<</Type /Page
/Parent 1 0 R
.....
但是如果我在 pdfService 之前停止使用调试器运行并查看 tmp/ 文件,它已经搞砸了..
1楼
是的,
您需要使用base64对文件进行编码,然后在收到接收到的文件时发送然后再次解码。 我发现它起作用的唯一方法。