提起された質問:
私は練習として小さなウェブサイトに取り組んでいますが、フロントエンドの画像はBase64によって暗号化され、デコードのためにバックエンドに渡されます。しかし、常に問題がありました、私にアドバイスをください
public static string base64toimg(string src)throws ioexception {string uuid = uuid.randomuuid()。toString(); StringBuilder NewPath = new StringBuilder(IMG_ROOT_PATH); NewPath.Append(セパレーター)。 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); newpath.toString()を返します。 } catch(ioexception e){new ioexception(); }}java.lang.illegalargumentexception:入力バイト配列には、4バイトの終了ユニットが間違っています
上記は関連する例外情報です。フロントエンドのbase64コードをメモ帳に貼り付けてから自分でデコードしようとしています。同じ問題も当てはまります。
解決:
IllegalargumentException:違法なパラメーター例外、
これを試してみてください、それは大丈夫です。
プロセスを教えてください:
Stackoverflow、デバッグを手に入れました。最後に、データがnullであることがわかりました。さあ、私たちはたくさん学ぶ必要があります。
次回問題のデバッグに遭遇したら、どのコードに問題があるかを確認してください。あなたに答えることによって、私は多くを学びました。
キーポイントはここにあります:新しいioException()をスローします。
try(outputStream out = new fileoutputStream(newPath.ToString())){out.write(data); } catch(ioexception e){e.printstacktrace();新しいruntimeexception( "これは例外がスローされた")を投げます); //新しい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(); } 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(); }最後に{if(out!= null){out.close(); }} newPath.ToString()を返します。 }