相关疑难解决方法(0)

PDF到字节数组,反之亦然

我需要将pdf转换为字节数组,反之亦然.

谁能帮我?

这就是我转换为字节数组的方式

public static byte[] convertDocToByteArray(String sourcePath) {

    byte[] byteArray=null;
    try {
        InputStream inputStream = new FileInputStream(sourcePath);


        String inputStreamToString = inputStream.toString();
        byteArray = inputStreamToString.getBytes();

        inputStream.close();
    } catch (FileNotFoundException e) {
        System.out.println("File Not found"+e);
    } catch (IOException e) {
                System.out.println("IO Ex"+e);
    }
    return byteArray;
}
Run Code Online (Sandbox Code Playgroud)

如果我使用以下代码将其转换回文档,则会创建pdf.但是它说'Bad Format. Not a pdf'.

public static void convertByteArrayToDoc(byte[] b) {          

    OutputStream out;
    try {       
        out = new FileOutputStream("D:/ABC_XYZ/1.pdf");
        out.close();
        System.out.println("write success");
    }catch (Exception e) {
        System.out.println(e);
    }
Run Code Online (Sandbox Code Playgroud)

java pdf arrays

27
推荐指数
5
解决办法
17万
查看次数

标签 统计

arrays ×1

java ×1

pdf ×1