Javamail API 를 이용해서 base64 인코딩하는 방법
| import com.sun.mail.util.BASE64EncoderStream; |
| import java.io.ByteArrayOutputStream; |
| import java.nio.charset.StandardCharsets; |
| import java.io.IOException; |
| |
| String text = "테스트"; |
| |
| String charset = StandardCharsets.UTF_8.name(); |
| ByteArrayOutputStream baos = null; |
| BASE64EncoderStream stream = null; |
| try { |
| baos = new ByteArrayOutputStream(); |
| stream = new BASE64EncoderStream(baos); |
| stream.write(text.getBytes(charset)); |
| stream.flush(); |
| String base64EncodedText = baos.toString(); |
| stream.close(); |
| stream = null; |
| baos.close(); |
| baos = null; |
| } catch (IOException e) { |
| |
| } finally { |
| if(stream != null) { |
| try { |
| stream.close(); |
| stream = null; |
| } catch (IOException e) { |
| |
| } |
| } |
| if(baos != null) { |
| try { |
| baos.close(); |
| baos = null; |
| } catch (IOException e) { |
| |
| } |
| } |
| } |
stream.flush();
를 생략하면, 마지막 글자 일부가 잘릴 가능성이 높다는 것에 주의하라.