【问题标题】:Saving PDF/Picture to Kubernetes configmap and retrieving it in springboot application将 PDF/图片保存到 Kubernetes configmap 并在 springboot 应用程序中检索
【发布时间】:2021-11-05 04:33:11
【问题描述】:

我需要将 pdf 存储在 kubernetes configmap 中,然后在 springboot 应用程序中检索它以进行进一步处理。 我使用pdf创建了一个configmap oc create configmap testpdf --from-file=datafile.pdf。 现在我有了带有 pdf 的 Base64 编码数据的 configmap,我已将其安装为卷安装。另外,我经历过当我尝试访问该配置映射时,它会自动从 Base64 解码回字节流(如果我错了,请纠正我)。 现在在我的 springboot 应用程序中使用检索配置映射数据。

    @Value(${datafile.pdf})
    byte[] pdfdata;

    public ResponseEntity<byte[]> retrievePDF() {
       HttpHeaders headers = new HttpHeaders();
       headers.setContentType(MediaType.APPLICATION_PDF);
       headers.set("Content-Disposition", "inline; filename=\"" + "terms_conditions.pdf\"");
       headers.setContentLength(pdfdata.length);
       return new ResponseEntity<>(pdfdata, headers, HttpStatus.OK);
    }

现在,当我点击控制器时,我得到一个空白的白色 pdf,尽管保存在 configmap 中的 pdf 非常好。 configmap中的Base64编码值也是正确的(我也检查过使用Base64在线pdf)。

请求帮助。过去一周我都陷入了这个困境。

【问题讨论】:

    标签: java spring-boot kubernetes openshift configmap


    【解决方案1】:

    试着写成这样的方法:

    byte[] pdfData = ...
    
    
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_PDF);
    
    headers.setContentDispositionFormData("terms_conditions.pdf", "terms_conditions.pdf");
    headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
    
    ResponseEntity<byte[]> response = new ResponseEntity<>(bytes, headers, HttpStatus.OK);
            
    return response;
    

    如果你的 pdfData 数组真的代表你的 PDF,它应该可以工作。

    【讨论】:

    • 嗨 Mihailo,尝试了您的建议。仍然是相同的空白pdf被下载。我认为我从 configmap 获得的字节数组在检索时被修改,或者应该有其他方式来存储来自 configmap 的 pdf 字节流。有什么建议或任何其他方式吗?请帮忙。
    • 是的,那么它肯定与配置映射(及其对pdf的存储)有关。让我试着理解:你想要一个配置映射,其中一个配置属性是pdf : &lt;base64data&gt;。使用您的 java 代码,您想访问代表 pdf 的属性吗?我认为您在问题中拥有的命令应该用于创建整个配置映射,参数--from-file 应该接受yaml。我明天可以检查一下,这听起来很有趣
    • 是的,这就是 configmap 存储的内容 ~pdf:~。在检索时,它正在解码,然后给我们价值。我使用的命令是直接从 pdf 文件创建配置映射,而不是使用 yaml。使用此命令,我得到的是一个配置映射,其内容为“”,因此该 pdf 文件名可以用作获取其值的键。希望你明白。请帮我解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 2022-01-20
    • 2011-10-15
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 2022-06-16
    • 2016-07-03
    • 1970-01-01
    相关资源
    最近更新 更多