1

apache.commons.codec jar ファイルを使用して、URL から取得した画像ファイルを Base64 文字列に変換しようとしています。

ジャバコード..

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 に答える