Convert Image to base64 String in Java
<p>I'm trying to convert an image file obtained from URL to Base64 String using apache.commons.codec jar file.</p>
<p><strong>Java code</strong>..</p>
<pre><code>import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.apache.commons.codec.binary.Base64;

public class HelloWorld {

public static void main(String args[]) {
    
     String imageUrl = &quot;http://www.avajava.com/images/avajavalogo.jpg&quot;;
        String destinationFile = &quot;image_1.jpg&quot;;

        try {           
            // Reading a Image file from file system
            URL url = new URL(imageUrl);
            InputStream is = url.openStream();

            BufferedInputStream imageInFile = new BufferedInputStream(url.openConnection().getInputStream());
            byte imageData[] = new byte[2048];
            imageInFile.read(imageData);

            // Converting Image byte array into Base64 String
            String imageDataString = encodeImage(imageData);
            System.out.println(&quot;imageDataString : &quot; + imageDataString);




            System.out.println(&quot;Image Successfully Manipulated!&quot;);
        } catch (FileNotFoundException e) {
            System.out.println(&quot;Image not found&quot; + e);
        } catch (IOException ioe) {
            System.out.println(&quot;Exception while reading the Image &quot; + ioe);
        }

}

public static String encodeImage(byte[] imageByteArray) {
    return Base64.encodeBase64URLSafeString(imageByteArray);
}
</code></pre>
<p>}</p>
<p>Whenever I run my code i get the following error</p>
<pre><code>Exception in thread &quot;main&quot; java.lang.Error: Unresolved compilation problem: 
The method encodeBase64URLSafeString(byte[]) is undefined for the type Base64

at helloWorld.HelloWorld.encodeImage(HelloWorld.java:51)
at helloWorld.HelloWorld.main(HelloWorld.java:35)
</code></pre>
<p>I can't understand where I'm going wrong even after importing the Base64 class.Please help..</p>
<p><strong>Edit 1</strong>:</p>
<p>The IDE is also giving the following error for method &quot;encodeImage&quot;</p>
<p><a href="https://i.stack.imgur.com/KRm0o.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KRm0o.png" alt="Error" /></a></p>
<p><strong>Edit 2:</strong></p>
<p>Java Build path of project
<a href="https://i.stack.imgur.com/oeLcA.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oeLcA.jpg" alt="build_path" /></a></p>