Preguntas planteadas:
Estoy trabajando en un pequeño sitio web como práctica, pero la imagen frontal está encriptada por Base64 y se pasa al back-end para decodificar. Pero siempre ha habido problemas, por favor dame consejos
public static string base64ToImg (String src) lanza ioexception {string uuid = uuid.randomuuid (). toString (); StringBuilder newpath = new StringBuilder (img_root_path); newpath.append (separador). 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 (datos); return newPath.ToString (); } catch (ioException e) {tirar nueva IOException (); }}java.lang.illegalargumentException: la matriz de bytes de entrada tiene una unidad final de 4 bytes
Los anteriores son información de excepción relacionada. Estoy tratando de pegar el código base64 del front-end en el bloc de notas y luego trato de decodificarlo yo mismo, y el mismo problema también es cierto.
Solución:
IlegalargumentException: excepción de parámetros ilegales,
Prueba esto, debería estar bien.
Déjame decirte el proceso:
Tengo Stackoverflow, depuración. Finalmente, encontré que los datos son nulos. Vamos, necesitamos aprender mucho.
La próxima vez que encuentre una depuración de problemas, vea qué código tiene un problema. Al responderte, he aprendido mucho.
El punto clave está aquí: tirar nueva IOException ();
try (outputStream out = new FileOutputStream (newpath.ToString ())) {out.write (data); } catch (ioException e) {E.PrintStackTrace (); tirar nueva runtimeException ("Esta es la excepción lanzada"); // tirar nueva runtimeException (e); } public static string base64ToImg (String src) lanza 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 (); }Agregue otro recurso de cierre comúnmente utilizado:
public static string base64ToImg (String src) lanza 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 (); prueba {out = new FileOutputStream (newpath.ToString ()); data = decoder.decode (src); out.write (datos); } catch (ioException e) {E.PrintStackTrace (); } finalmente {if (out! = null) {out.close (); }} return newPath.ToString (); }