Android代码正确,但Base64字符串转pdf时报错(已解决)

转pdf核心代码

public static void base64StringToPdf(String base64Content,String filePath,Base64Listen base64Listen){
        if (TextUtils.isEmpty(base64Content)){  
            return;
        }
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        try {
            // 将base64编码的字符串解码成字节数组
            byte[] bytes = Base64.decode(base64Content, Base64.DEFAULT);    //base64编码内容转换为字节数组:DEFAULT
            //创建一个将bytes作为其缓冲区的ByteArrayInputStream对象
            ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
            //创建从底层输入流中读取数据的缓冲输入流对象
            bis = new BufferedInputStream(byteInputStream);
            // 指定输出的文件
            File file = new File(filePath);
            File path = file.getParentFile();
            if(!path.exists()){
                path.mkdirs();
            }
            // 创建到指定文件的输出流
            fos = new FileOutputStream(file);
            // 为文件输出流对接缓冲输出流对象
            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();
            base64Listen.onSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            base64Listen.onFail(e.toString()+"");
        }finally{
            try {
                if (bis != null){
                    bis.close();
                }
                if (fos != null){
                    fos.close();
                }
                if (bos != null){
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                base64Listen.onFail(e.toString()+"");
            }

        }
    }

成功失败接口

public interface Base64Listen{
        void onSuccess();
        void onFail(String hint);
    }

base64一直转失败的原因

当你发现你代码没问题,但就是不成功,很可能问题其它原因导致

  • base64数据本身就有问题,查看方式就是看看接口返回的数据是否有“+”号;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PYB3

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值