2016-05-05 9 views
1

apache.commons.codec jarファイルを使用して、URLから取得したイメージファイルをBase64 Stringに変換しようとしています。私は私のコードを実行するたびにイメージをbase64に変換するJavaの文字列

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"

Error

編集2:プロジェクトの

Javaのビルド・パス build_path

+0

Hmm。どのバージョンのcodec.jarをお持ちですか?その方法は '@since 1.4'です。 – Thilo

+0

私はこのリンクで利用可能なバージョン1.10を使用しています。https://commons.apache.org/proper/commons-codec/download_codec.cgi – Lucy

+0

このエラーは発生していません。コンパイラのクラスパスにあるものを再確認できますか? – Thilo

答えて

0

documentationによれば、バージョン1.4で追加されました。

使用しているJarのバージョンを再度確認してください。

+1

ルーシーが構築できないように見えません。それは "未解決のコンパイルの問題"と言います。それはビルド時のエラーです。 – Thilo

+0

速く読む。 Thiloありがとう。更新されます。 – gfelisberto

+0

さて、コンパイルできなかった実際のコード(ビルド時に報告される)の代わりにコンパイラによって作成されたスタブコードが見つかった場合、より正確に言えば、ランタイムエラーです。 – Thilo

関連する問題