Java Base64解码错误及解决方法
作者:laozhang 发布时间:2023-02-09 03:36:35
标签:Java,Base64,解码错误
问题提出:
自己在做一个小网站充当练手,但是前端图片经过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();
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();
}


猜你喜欢
- 1、系统信息:Windows10 64位2、环境搭建参考:https://www.jb51.net/article/185086.htm3、
- 大多数android程序员应该都知道genymotion是一个不错的模拟器,体积小巧,启动速度快。相关的博客也比较多,但是一直以来无法解决a
- 先上一张效果图using UnityEngine;using System.Collections;public class TestRot
- 开发环境 android studio 3.0.1 已支持 kotlin1、定义接口interface CallBack{ fun call
- 本文实例讲述了C#实现的SQL备份与还原功能。分享给大家供大家参考,具体如下://记得加 folderBrowserDialog1 open
- Java计算一段程序的运行时间介绍了两种方法,一种是毫秒级别的计算,另一种是更精确的纳秒级别的计算。毫秒级别计算时间  
- 一、配置xml路径mybatis-plus:mapper-locations: classpath:mapper/*.xml二、编写Mapp
- 目录引言简单遍历筛选符合某属性条件的List集合获取某属性返回新的List集合获取以某属性为key,其他属性或者对应对象为value的Map
- 在网上看到一个进度条效果图,非常美观,如下:进行效果分解:1.渐变色,看起来颜色变化并不复杂,使用LinearGradient应该可以实现。
- 参考链接:狂神的Swagger笔记号称世界上最流行的API框架Restful Api 文档在线自动生成器 => API 文档 与API
- 主题 ActiveMQ Spring Boot 小程序开发1.引入依赖<parent><groupId>org.sp
- 前言老了,老了,那天有位小同事问我Android跳转三方应用时有什么要注意的?是否可以直接跳?如何传递参数过去? 嗯… 我竟然说需要root
- 本文实例为大家分享了Android实现简单旋转动画的具体代码,供大家参考,具体内容如下核心方法public void startAnimat
- 通常的函数调用 一个通常的函数调用的例子://自行包含头文件void MyFun(int x); //此
- 本文将介绍使用Spring Boot集成Mybatis并实现主从库分离的实现(同样适用于多数据源)。延续之前的Spring Boot 集成M
- asp.net页面中如何获取Excel表的内容,具体内容介绍如下所示:首先引用组件和命名空间using Microsoft.Office.I
- Logback 背景Logback是由log4j创始人设计的另一个开源日志组件,官方网站:http://logback.qos.ch。它当前
- 本文实例为大家分享了Java实现小型图书馆管理系统的具体代码,供大家参考,具体内容如下以下为小型图书馆管理系统模式图:模式总体概述:其中IB
- Java集合的主要分为三种类型:• Set(集)• List(列表)• Map(映射)要深入理解集合首先要了解
- The java.io.Writer.flush() method flushes the stream. If the stream ha