超长base64字符串转pdf文件

这个时候最好直接从文件读取

/**
     * 超长base64字符串转pdf文件
     * @param filePath
     * @throws IOException
     */
    public static void base64StringToPdf( String filePath) throws IOException {
        File file1 = new File("D://base64.txt");
        InputStream in = new FileInputStream(file1);
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] bytes = new byte[0];
        try {
            bytes = decoder.decodeBuffer(in);
        } catch (IOException e) {

        }
        File file = new File(filePath);
        File path = file.getParentFile();
        if (!path.exists()) {
            boolean b = path.mkdirs();
            if (!b) {

            }
        }
        try{
            ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
            BufferedInputStream bis = new BufferedInputStream(byteInputStream);
            FileOutputStream fos = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(fos);

            byte[] buffer = new byte[1024];
            int length = bis.read(buffer);
            while (length != -1) {
                bos.write(buffer, 0, length);
                length = bis.read(buffer);
            }
            bos.flush();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容