PDF to byte array and vice versa

前端 未结 13 1131
独厮守ぢ
独厮守ぢ 2020-11-27 04:19

I need to convert pdf to byte array and vice versa.

Can any one help me?

This is how I am converting to byte array

public static byte[] conve         


        
相关标签:
13条回答
  • 2020-11-27 04:57

    Java 7 introduced Files.readAllBytes(), which can read a PDF into a byte[] like so:

    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.nio.file.Files;
    
    Path pdfPath = Paths.get("/path/to/file.pdf");
    byte[] pdf = Files.readAllBytes(pdfPath);
    

    EDIT:

    Thanks Farooque for pointing out: this will work for reading any kind of file, not just PDFs. All files are ultimately just a bunch of bytes, and as such can be read into a byte[].

    0 讨论(0)
提交回复
热议问题