Java Base64解码错误及解决方法

作者:laozhang 时间:2023-02-09 03:36:35 

问题提出:

自己在做一个小网站充当练手,但是前端图片经过base64加密后传往后端在解码。但是一直都有问题,请大神赐教


 public static String base64ToImg(String src) throws IOException {
   String uuid = UUID.randomUUID().toString();
   StringBuilder newPath = new StringBuilder(IMG_ROOT_PATH);
   newPath.append(separator).
       append(uuid).
       append(IMG_SUFFIX);
   if(src == null){
     return null;
   }
   byte[] data = null;
   Base64.Decoder decoder = Base64.getDecoder();
   try (OutputStream out = new FileOutputStream(newPath.toString())) {
     data = decoder.decode(src);
     out.write(data);
     return newPath.toString();
   } catch (IOException e) {
     throw new IOException();
   }
 }

java.lang.IllegalArgumentException: Input byte array has wrong 4-byte ending unit

以上是相关的异常信息。我试图将前端的base64码粘贴到记事本然后自己在试着解码,也是同样问题。

解决办法:

IllegalArgumentException:非法参数异常,

试下这个,应该可以。

给你讲述下过程:

去了stackoverflow,debug。最后发现data为null,,加油吧,我们需要学的还很多

下次遇到问题debug下,看是哪条代码出现问题了,通过回答你,我也学到了很多

关键点在这里: throw new IOException();

Java Base64解码错误及解决方法


try (OutputStream out = new FileOutputStream(newPath.toString())) {
     out.write(data);
   } catch (IOException e) {
     e.printStackTrace();
     throw new RuntimeException("异常是这么抛出的");
     //throw new RuntimeException(e);
   }

public static String base64ToImg(String src) throws IOException {
   String uuid = UUID.randomUUID().toString();
   StringBuilder newPath = new StringBuilder("xx");
   newPath.append("xx").
       append(uuid).
       append("xx");
   if (src == null) {
     return null;
   }
   byte[] data = Base64.getDecoder().decode(src);
   try (OutputStream out = new FileOutputStream(newPath.toString())) {
     out.write(data);
   } catch (IOException e) {
     e.printStackTrace();
   }
   return newPath.toString();
 }

补充另外一种常用关闭资源:


public static String base64ToImg(String src) throws IOException {
   String uuid = UUID.randomUUID().toString();
   StringBuilder newPath = new StringBuilder("xx");
   newPath.append("xx").
       append(uuid).
       append("xx");
   if (src == null) {
     return null;
   }
   byte[] data = null;
   OutputStream out = null;
   Base64.Decoder decoder = Base64.getDecoder();
   try {
     out = new FileOutputStream(newPath.toString());
     data = decoder.decode(src);
     out.write(data);
   } catch (IOException e) {
     e.printStackTrace();
   } finally {
     if (out != null) {
       out.close();
     }
   }
   return newPath.toString();
 }
标签:Java,Base64,解码错误
0
投稿

猜你喜欢

  • C#实现添加多行文本水印到Word文档

    2023-03-22 07:45:33
  • C/C++混合编程之extern “C”的使用示例

    2021-11-18 03:33:58
  • SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到解决方案

    2021-08-26 12:54:32
  • 解决SpringBoot运行Test时报错:SpringBoot Unable to find

    2021-11-15 16:48:56
  • C#中timer定时器用法实例

    2023-01-23 02:23:17
  • SpringCloud可视化链路追踪系统Zipkin部署过程

    2023-11-27 04:34:06
  • Android之日期及时间选择对话框用法实例分析

    2023-06-26 23:59:34
  • C# memcached缓存使用实例代码

    2022-01-15 02:17:11
  • Android应用中炫酷的横向和环形进度条的实例分享

    2023-09-21 17:33:08
  • 解析Java中PriorityQueue优先级队列结构的源码及用法

    2023-11-08 13:33:11
  • 深入c# Func委托的详解

    2022-02-15 05:44:51
  • Java 实战项目锤炼之网上花店商城的实现流程

    2021-09-14 04:51:45
  • java 实现将Object类型转换为int类型

    2023-11-11 07:58:03
  • SpringBoot 集成 Memcached的方法示例

    2021-11-30 18:29:20
  • 关于Maven混合配置私有仓库和公共仓库的问题

    2021-10-15 20:30:51
  • Unity3d 使用Gizmos画一个圆圈

    2022-05-22 05:10:52
  • Java实现文件读取和写入过程解析

    2023-06-28 11:35:36
  • 初识Spring Boot框架之Spring Boot的自动配置

    2022-08-25 10:27:57
  • Mybatis关于动态排序 #{} ${}问题

    2023-09-01 17:34:57
  • Winform之TextBox输入日期格式验证yyyy-mm-dd

    2023-04-14 21:35:05
  • asp之家 软件编程 m.aspxhome.com