أثيرت الأسئلة:
أعمل على موقع ويب صغير كممارسة ، لكن الصورة الأمامية مشفرة بواسطة BASE64 ويتم تمريرها إلى الواجهة الخلفية لفك تشفيرها. ولكن كانت هناك دائمًا مشاكل ، من فضلك أعطني نصيحة
static Static String Base64ToImg (String SRC) يلقي ioException {String uuid = uuid.randomuuid (). toString () ؛ StringBuilder newPath = جديد 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 = newOutputStream (newPath.ToString ())) {data = decoder.decode (src) ؛ out.write (البيانات) ؛ إرجاع newPath.ToString () ؛ } catch (ioException e) {ream new ioException () ؛ }}java.lang.illegalargumentexception: صفيف البايت الإدخال لديه وحدة نهاية 4 بايت خاطئ
ما ورد أعلاه هي معلومات الاستثناء المرتبطة. أحاول لصق رمز BASE64 للواجهة الأمامية في المفكرة ثم أحاول فك تشفيره بنفسي ، وآخر المشكلة صحيحة أيضًا.
حل:
غير unalfalArgumentException: استثناء معلمة غير قانوني ،
جرب هذا ، يجب أن يكون على ما يرام.
دعني أخبرك بالعملية:
حصلت على stackoverflow ، تصحيح. أخيرًا ، وجدت أن البيانات لاغية. تعال ، نحن بحاجة إلى تعلم الكثير.
في المرة القادمة أواجه مشكلة تصحيح ، راجع الرمز الذي لديه مشكلة. من خلال الرد عليك ، لقد تعلمت الكثير.
النقطة الرئيسية هنا: رمي ioException () جديدة ؛
Try (outputStream Out = new FileOutputStream (newPath.ToString ())) {out.write (data) ؛ } catch (ioException e) {E.PrintStackTrace () ؛ رمي New RunTimeException ("هذا هو الاستثناء الذي تم إلقاؤه") ؛ // رمي new RunTimeException (e) ؛ } static Static String Base64ToImg (String SRC) يلقي ioException {String uuid = uuid.randomuuid (). toString () ؛ StringBuilder NewPath = جديد 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 () ؛ }أضف مورد إيقاف آخر شائع الاستخدام:
static Static String Base64ToImg (String SRC) يلقي ioException {String uuid = uuid.randomuuid (). toString () ؛ StringBuilder NewPath = جديد StringBuilder ("XX") ؛ NewPath.Append ("XX"). إلحاق (UUID). إلحاق ("xx") ؛ if (src == null) {return null ؛ } byte [] data = null ؛ OutputStream Out = null ؛ base64.decoder decoder = base64.getDecoder () ؛ حاول {out = new FileOutputStream (newPath.ToString ()) ؛ data = decoder.decode (src) ؛ out.write (البيانات) ؛ } catch (ioException e) {E.PrintStackTrace () ؛ } أخيرًا {if (out! = null) {out.close () ؛ }} return newPath.ToString () ؛ }