[JAVA] Image File to Base64 String (이미지 파일을 Base64 문자열로 변환)

[JAVA] Image File to Base64 String (이미지 파일을 Base64 문자열로 변환)

    String strBase64 = “”;

   

    File f = new File(filePath);
    if (f.exists() && f.isFile() && f.length() > 0) {
        byte[] bt = new byte[(int) f.length()];
        FileInputStream fis = null;

        try {
            fis = new FileInputStream(f);
            fis.read(bt);
            strBase64 = new String(Base64.encodeBase64(bt));

           

        } catch (Exception e) {
            throw e;
           
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
            } catch (Exception e) {
        }
    }