日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
JavaBase64解碼錯誤怎么辦

這篇文章將為大家詳細(xì)講解有關(guān)Java Base64解碼錯誤怎么辦,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

我們提供的服務(wù)有:做網(wǎng)站、成都網(wǎng)站制作、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、井研ssl等。為上1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的井研網(wǎng)站制作公司

問題提出:

自己在做一個小網(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ù)異常,

試下這個,應(yīng)該可以。

給你講述下過程:

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

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

關(guān)鍵點(diǎn)在這里: 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();
  }

補(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)于“Java Base64解碼錯誤怎么辦”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。


文章題目:JavaBase64解碼錯誤怎么辦
網(wǎng)頁URL:http://www.dlmjj.cn/article/ghhehs.html