Java Base64解碼錯(cuò)誤及解決方法
問題提出:
自己在做一個(gè)小網(wǎng)站充當(dā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
以上是相關(guān)的異常信息。我試圖將前端的base64碼粘貼到記事本然后自己在試著解碼,也是同樣問題。
解決辦法:
IllegalArgumentException:非法參數(shù)異常,
試下這個(gè),應(yīng)該可以。
給你講述下過程:
去了stackoverflow,debug。最后發(fā)現(xiàn)data為null,,加油吧,我們需要學(xué)的還很多
下次遇到問題debug下,看是哪條代碼出現(xiàn)問題了,通過回答你,我也學(xué)到了很多
關(guān)鍵點(diǎ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(); }
補(bǔ)充另外一種常用關(guān)閉資源:
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(); }
相關(guān)文章
springboot獲取properties屬性值的多種方式總結(jié)
這篇文章主要介紹了springboot獲取properties屬性值的多種方式總結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03解決springboot整合cxf-jaxrs中json轉(zhuǎn)換的問題
這篇文章主要介紹了解決springboot整合cxf-jaxrs中json轉(zhuǎn)換的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Struts2學(xué)習(xí)筆記(7)-訪問Web元素
這篇文章主要介紹Struts2中訪問Web元素的方法,希望能給大家做一個(gè)參考。2016-06-06詳解Java線程池隊(duì)列中的延遲隊(duì)列DelayQueue
這篇文章主要為大家詳細(xì)介紹了Java線程池隊(duì)列中的延遲隊(duì)列DelayQueue的相關(guān)資料,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-12-12使用Java DOM解析器修改XML文件內(nèi)容的操作方法
在Java中,XML文件的解析和修改可以通過多種方法實(shí)現(xiàn),其中DOM(Document Object Model)是一種常用的方式,在本文中,我們將介紹如何使用Java DOM解析器修改XML文件中的內(nèi)容,并給出一個(gè)具體的示例,需要的朋友可以參考下2024-08-08Spring?Cloud中Sentinel的兩種限流模式介紹
如何使用Sentinel做流量控制呢?這篇文章就來為大家詳細(xì)介紹了Spring?Cloud中Sentinel的兩種限流模式,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-05-05