PDF to byte array and vice versa

前端 未结 13 1210
独厮守ぢ
独厮守ぢ 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:40

    The problem is that you are calling toString() on the InputStream object itself. This will return a String representation of the InputStream object not the actual PDF document.

    You want to read the PDF only as bytes as PDF is a binary format. You will then be able to write out that same byte array and it will be a valid PDF as it has not been modified.

    e.g. to read a file as bytes

    File file = new File(sourcePath);
    InputStream inputStream = new FileInputStream(file); 
    byte[] bytes = new byte[file.length()];
    inputStream.read(bytes);
    

提交回复
热议问题