java base64 损坏_Java Base64解码错误及解决方法

在尝试将前端base64加密的图片解码到后端时遇到`IllegalArgumentException: Input byte array has wrong 4-byte ending unit`异常。问题出在`data`变量为null,通过修正代码并确保在解码前检查源字符串不为空,成功解决异常。学习了在遇到问题时进行debug的重要性,并提供了两种不同的资源关闭方式。
摘要由CSDN通过智能技术生成

问题提出:

自己在做一个小网站充当练手,但是前端图片经过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();

5f160b161f6a7bd071581b8b0230f508.png

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();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果Base64编码字符串太长导致Java解码错误,可以考虑以下两种解决方法: 1. 分割字符串 可以将Base64编码字符串按照一定长度进行分割,然后分别进行解码,最后将解码后的结果拼接起来即可。例如,将字符串每隔76个字符分割一次: ``` String base64Str = "long_base64_string"; int len = base64Str.length(); int splitLen = 76; StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i += splitLen) { int endIndex = Math.min(i + splitLen, len); String subStr = base64Str.substring(i, endIndex); byte[] bytes = Base64.getDecoder().decode(subStr); sb.append(new String(bytes, StandardCharsets.UTF_8)); } String result = sb.toString(); ``` 2. 使用其他编码方式 如果Base64编码字符串实在太长,可以考虑使用其他编码方式,例如gzip压缩后再进行Base64编码,可以大幅减小字符串长度。解码时先进行Base64解码,再进行gzip解压缩即可。例如: ``` String str = "long_string_to_compress"; byte[] compressed = compress(str.getBytes(StandardCharsets.UTF_8)); String encodedStr = Base64.getEncoder().encodeToString(compressed); // 解码byte[] decoded = Base64.getDecoder().decode(encodedStr); byte[] decompressed = decompress(decoded); String result = new String(decompressed, StandardCharsets.UTF_8); // gzip压缩 private static byte[] compress(byte[] data) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (GZIPOutputStream gzos = new GZIPOutputStream(baos)) { gzos.write(data); } return baos.toByteArray(); } // gzip解压缩 private static byte[] decompress(byte[] data) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (GZIPInputStream gzis = new GZIPInputStream(new ByteArrayInputStream(data))) { byte[] buffer = new byte[1024]; int len; while ((len = gzis.read(buffer)) > 0) { baos.write(buffer, 0, len); } } return baos.toByteArray(); } ``` 以上是解决Base64编码字符串过长的两种方法,具体选择哪种方法取决于实际情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值