คำถามที่เกิดขึ้น:
ฉันกำลังทำงานบนเว็บไซต์เล็ก ๆ เพื่อฝึกฝน แต่ภาพส่วนหน้าถูกเข้ารหัสโดย Base64 และถูกส่งผ่านไปยังส่วนหลังเพื่อถอดรหัส แต่มีปัญหาอยู่เสมอโปรดให้คำแนะนำแก่ฉัน
สตริงคงที่สาธารณะ base64toimg (สตริง 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 (); ลอง (outputStream out = new fileOutputStream (newPath.toString ())) {data = decoder.decode (SRC); out.write (ข้อมูล); กลับ newpath.tostring (); } catch (ioexception e) {โยน iOexception ใหม่ (); -java.lang.illegalargumentException: อินพุตอาร์เรย์อาร์เรย์มีหน่วยสิ้นสุด 4 ไบต์ที่ไม่ถูกต้อง
ข้างต้นเป็นข้อมูลข้อยกเว้นที่เกี่ยวข้อง ฉันพยายามที่จะวางรหัส base64 ของส่วนหน้าลงในแผ่นจดบันทึกแล้วพยายามถอดรหัสด้วยตัวเองและปัญหาเดียวกันก็เป็นจริงเช่นกัน
สารละลาย:
การผิดกฎหมาย EllegalArgumentException: ข้อยกเว้นพารามิเตอร์ที่ผิดกฎหมาย
ลองสิ่งนี้มันควรจะโอเค
ให้ฉันบอกกระบวนการ:
มี Stackoverflow, Debug ในที่สุดฉันก็พบว่าข้อมูลเป็นโมฆะ มาเลยเราต้องเรียนรู้มากมาย
ครั้งต่อไปที่ฉันพบปัญหาการดีบักดูว่ารหัสใดมีปัญหา โดยการตอบคุณฉันได้เรียนรู้มากมาย
ประเด็นสำคัญคือที่นี่: โยน iOexception ใหม่ ();
ลอง (outputStream out = new fileOutputStream (newPath.toString ())) {out.write (ข้อมูล); } catch (ioexception e) {e.printstacktrace (); โยน runtimeException ใหม่ ("นี่คือข้อยกเว้นที่โยน"); // โยน runtimeException ใหม่ (E); - สตริงคงที่สาธารณะ base64toimg (สตริง 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); ลอง (outputStream out = new fileOutputStream (newPath.toString ())) {out.write (ข้อมูล); } catch (ioexception e) {e.printstacktrace (); } return newpath.toString (); -เพิ่มทรัพยากรปิดการปิดที่ใช้กันทั่วไปอื่น ๆ :
สตริงคงที่สาธารณะ base64toimg (สตริง 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 (); ลอง {out = ใหม่ fileOutputStream (newPath.toString ()); data = decoder.decode (SRC); out.write (ข้อมูล); } catch (ioexception e) {e.printstacktrace (); } ในที่สุด {ถ้า (ออก! = null) {out.close (); }} ส่งคืน newpath.toString (); -