제기 된 질문 :
나는 작은 웹 사이트에서 연습으로 작업하고 있지만 프론트 엔드 이미지는 Base64에 의해 암호화되며 디코딩을 위해 백엔드로 전달됩니다. 하지만 항상 문제가 있었는데 조언을 해주세요
public static string base64toimg (String src)는 ioexception {string uuid = uuid.randomuuid (). toString (); StringBuilder newPath = New StringBuilder (img_root_path); NewPath.Append (분리기). 부록 (uuid). 부록 (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 (데이터); return newPath.toString (); } catch (ioException e) {throw new ioException (); }}java.lang.ilegalargumentexception : 입력 바이트 어레이에는 4 바이트 엔딩 유닛이 잘못되었습니다
위는 관련 예외 정보입니다. 프론트 엔드의 Base64 코드를 메모장에 붙여 넣은 다음 직접 해독하려고 노력하고 있으며 같은 문제도 마찬가지입니다.
해결책:
불법적 인 매개 변수 예외,
이것을 시도해보십시오. 괜찮을 것입니다.
프로세스를 알려 드리겠습니다.
stackoverflow, debug가 있습니다. 마지막으로 데이터가 무효임을 알았습니다. 어서, 우리는 많은 것을 배워야합니다.
다음에 문제 디버그가 발생하면 어떤 코드가 문제가 있는지 확인하십시오. 당신에게 대답함으로써, 나는 많은 것을 배웠습니다.
핵심 요점은 다음과 같습니다. 새로운 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)는 ioexception {string uuid = uuid.randomuuid (). toString (); StringBuilder NewPath = New StringBuilder ( "xx"); NewPath.Append ( "XX"). 부록 (uuid). 부록 ( "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)는 ioexception {string uuid = uuid.randomuuid (). toString (); StringBuilder NewPath = New StringBuilder ( "xx"); NewPath.Append ( "XX"). 부록 (uuid). 부록 ( "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 (데이터); } catch (ioexception e) {e.printstacktrace (); } 마침내 {if (out! = null) {out.close (); }} return newPath.toString (); }