PDF to byte array and vice versa

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

    This works for me:

    try(InputStream pdfin = new FileInputStream("input.pdf");OutputStream pdfout = new FileOutputStream("output.pdf")){
        byte[] buffer = new byte[1024];
        int bytesRead;
        while((bytesRead = pdfin.read(buffer))!=-1){
            pdfout.write(buffer,0,bytesRead);
        }
    }
    

    But Jon's answer doesn't work for me if used in the following way:

    try(InputStream pdfin = new FileInputStream("input.pdf");OutputStream pdfout = new FileOutputStream("output.pdf")){
    
        int k = readFully(pdfin).length;
        System.out.println(k);
    }
    

    Outputs zero as length. Why is that ?

提交回复
热议问题