public class Test2 { public static String get() throws IOException { InputStream resourceAsStream = Test2.class.getResourceAsStream("/image/image_1.png"); BufferedInputStream bufferedInputStream = new BufferedInputStream(resourceAsStream); byte[] bytes = new byte[bufferedInputStream.available()]; bufferedInputStream.read(bytes); BASE64Encoder base64Encoder = new BASE64Encoder(); String encode = base64Encoder.encode(bytes); System.out.println(encode); bufferedInputStream.close(); return encode; } }