1

我正在尝试使用 apache.commons.codec jar 文件将从 URL 获得的图像文件转换为 Base64 字符串。

Java代码..

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);
}

}

每当我运行我的代码时,我都会收到以下错误

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)

即使在导入 Base64 类后,我也无法理解哪里出错了。请帮助..

编辑 1

IDE 还为方法“encodeImage”提供以下错误

错误

编辑2:

项目的Java构建路径 build_path

4

1 回答 1

0

根据文档,该方法是在 1.4 版中添加的。

仔细检查您正在使用的 Jar 的版本。

于 2016-05-05T09:07:03.000 回答