首页 > 解决方案 > 将 byte[] 转换为 PDF 文件而不用 Java 保存在本地磁盘中

问题描述

我将字节数组作为输入,它应该转换为 PDF 类型文件而不写入本地系统,这是我的代码:

public File convertToFile(byte[] data) throws IOException, NullPointerException {
        
        FileOutputStream bar = null;
        String pathname = "filename.pdf";
        File file = new File(pathname);
        try (ByteArrayInputStream bais = new ByteArrayInputStream(data);
            ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length)) {
            bar = new FileOutputStream(file);
            int c = 0;
            while (c < bais.read()) {
                baos.write(data);
                baos.writeTo(bar);``
                baos.flush();
            }
        } catch (Exception e) {
            e.getMessage();
        }
        return file;
    }

当我运行此代码时,它会返回我的本地路径。

标签: javapdf

解决方案


推荐阅读