首页 > 解决方案 > How to return a file from a controller inside an entity?

问题描述

I tried to return .pfx certificate as bytearray

public class CertificateContainer {

    private CertificateType certificateType;
    private byte[] certificate;
    private Map<String, String> authData;
}

converting into bytearray

FileUtils.readFileToByteArray(ResourceUtils.getFile(certificateType.getPathToCertificate()))

And the controller's return

    @GetMapping
    public ResponseEntity<CertificateContainer> getCertificate(@RequestParam CertificateType certificateType) {
        return ok(certificateService.getCertificate(certificateType));
    }

On a client's side, I'm getting String instead of bytes and adding it to an array

new ByteArrayInputStream(
                certificateContainer.getCertificate().getBytes()
        )

after that, adding into key store and getting exception "stream does not represent PKCS12 key store". But if I add the same file from the client storage, it works properly. What I did wrong with transporting the file?

标签: javaspring-boot

解决方案


推荐阅读