java-从以字符串形式出现的字节下载pdf?

rkkpypqq  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(333)

我们正在调用一些api,它返回字节作为字符串,我需要从字节流(作为字符串)下载pdf。
例如

download_pdf":[{"name":"Transaction_Details.pdf","value":"JVBERi0xLjQNCiXi48/TDQoyIDAgb2JqCjw8L051bXNbMCAzIDAgUl..."}]

当试图从中写入文件时,它会损坏并且无法打开文件。

try {
                    return ResponseEntity.ok()
                            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" +item.getName())
                            .body(item.getValue().getBytes());

                } catch (Exception e) {
                    e.printStackTrace();
                }

下面的代码也试过了,但没有成功。

InputStream inputStream = new ByteArrayInputStream(item.getValue().getBytes()(Charset.forName("UTF-8")));

如何从file/bytes(以字符串形式出现)创建inputstream或byte[]?

xyhw6mcr

xyhw6mcr1#

结构是api响应,它不属于我们,因此在这方面无法提供帮助。但正如谨慎乐观主义者所建议的那样,其中一个注解base64解码解决了这个问题。我接受他的回答,并添加新的评论。

Base64 decoder = new Base64();
                    byte[] decodedBytes = decoder.decode(item.getValue());
                    return ResponseEntity.ok()
                            .header(HttpHeaders.CONTENT_DISPOSITION, ATTACHMENT_FILENAME +item.getName())
                            .body(decodedBytes);```

相关问题