[TIL] Java7 에서는 java.util.Base64 가 동작하지 않는다

Funnystyle·2021년 3월 30일
0

테스트 서버는 java 1.8 인데 운영서버는 java 1.7 인 환경에서 운영서버에 올렸더니 먹통이 되는 Base64 encode/decode...

검색해보니, java 1.7 에서는 java.util.Base64 encode/decode 가 안된단다... 헐.

apache.commons.codec 을 이용할 것.

import org.apache.commons.codec.binary.Base64;

public class Codec {
  public static void main(String[] args) {
    try {
      String clearText = "Hello world";
      String encodedText;

      // Base64
      encodedText = new String(Base64.encodeBase64(clearText.getBytes()));
      System.out.println("Encoded: " + encodedText);
      System.out.println("Decoded:"
          + new String(Base64.decodeBase64(encodedText.getBytes())));
      //
      // output :
      //   Encoded: SGVsbG8gd29ybGQ=
      //   Decoded:Hello world
      //
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

참고:
https://stackoverflow.com/questions/34469472/use-of-base64-class-in-jdk7#34469515

profile
polyglot

1개의 댓글

comment-user-thumbnail
2023년 1월 4일

감사합니다~!

답글 달기