1

I'm trying to convert an image file obtained from URL to Base64 String using apache.commons.codec jar file.

Java 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 = "http://www.avajava.com/images/avajavalogo.jpg";
        String destinationFile = "image_1.jpg";

        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("imageDataString : " + imageDataString);




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

}

public static String encodeImage(byte[] imageByteArray) {
    return Base64.encodeBase64URLSafeString(imageByteArray);
}

}

Whenever I run my code i get the following error

Exception in thread "main" 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)

I can't understand where I'm going wrong even after importing the Base64 class.Please help..

Edit 1:

The IDE is also giving the following error for method "encodeImage"

Error

Edit 2:

Java Build path of project build_path

Community
  • 1
  • 1
Lucy
  • 1,812
  • 15
  • 55
  • 93

1 Answers1

0

According to the documentation that method was added in version 1.4.

Double check the version of the Jar you are using.

gfelisberto
  • 1,655
  • 11
  • 18
  • 1
    It does not look like Lucy can build it. It says "unresolved compilation problem". That's a build-time error. – Thilo May 05 '16 at 09:07
  • Okay, to be more precise, it *is* a runtime error, when you encounter stub code created by the compiler instead of the real code that it could not compile (which it will have reported at build time). – Thilo May 05 '16 at 09:09