日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發(fā)布時間:2023/12/10 java 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java base64解码出错_Java Base64解码错误及解决方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

問題提出:

自己在做一個小網(wǎng)站充當練手,但是前端圖片經(jīng)過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:非法參數(shù)異常,

試下這個,應該可以。

給你講述下過程:

去了stackoverflow,debug。最后發(fā)現(xiàn)data為null,,加油吧,我們需要學的還很多

下次遇到問題debug下,看是哪條代碼出現(xiàn)問題了,通過回答你,我也學到了很多

關鍵點在這里: 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();

}

希望與廣大網(wǎng)友互動??

點此進行留言吧!

總結(jié)

以上是生活随笔為你收集整理的java base64解码出错_Java Base64解码错误及解决方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。