2014-06-25 62 views
2

我想對Base64中的URL圖像進行編碼和保存。 我發現了幾個例子,從本地文件進行編碼,但不是從URL進行編碼。 有沒有可能做到這一點?使用Java在Base64中對URL進行編碼

我試過類似的東西,但沒有成功。任何線索,幫助? 感謝您的回答。

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

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

     FileInputStream imageInFile = new FileInputStream(is.toString()); 
     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); 
    } 

} 

/** 
* Encodes the byte array into base64 string 
* 
* @param imageByteArray - byte array 
* @return String a {@link java.lang.String} 
*/ 
public static String encodeImage(byte[] imageByteArray) { 
    return Base64.encodeBase64URLSafeString(imageByteArray); 
} 
+0

'FileInputStream imageInFile = new FileInputStream(is.toString());'這是錯誤的,請閱讀API文檔,確保您完全理解代碼中每一行的內容。 – Jesper

+0

我知道這是錯誤的部分。我拿一個文件來做例子。但我試圖用URL和InputStream –

回答

2

您不應該使用FileInputStream。

使用類似:

URL url = new URL(imageUrl); 
BufferedInputStream bis = new BufferedInputStream(url.openConnection().getInputStream()); 

而且你需要在循環,直到你讀出的圖像的所有字節讀取數據。

0

你明白這行嗎?

FileInputStream imageInFile = new FileInputStream(is.toString()); 

首先它會調用InputStream對象toString。這將導致字符串看起來像這樣:[email protected]。然後它會嘗試打開一個具有該名稱的文件。你不想讀取名爲[email protected]的文件,所以這是完全錯誤的。

你想要做的是將原來的InputStream is中的所有字節讀入一個字節數組。如何做到這一點是在這個問題的答案解釋:

Convert InputStream to byte array in Java

+1

來解釋它。最後,我使用了類似的東西.'url = new URL(imageUrl); InputStream is = url.openStream(); \t \t byte [] bytes = IOUtils.toByteArray(is); imageDataString = encodeImage(bytes);' –

1

通過將圖像的URL參數試試這個功能。

private String getByteArrayFromImageURL(String url) { 

    try { 
     URL imageUrl = new URL(url); 
     URLConnection ucon = imageUrl.openConnection(); 
     InputStream is = ucon.getInputStream(); 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     byte[] buffer = new byte[1024]; 
     int read = 0; 
     while ((read = is.read(buffer, 0, buffer.length)) != -1) { 
      baos.write(buffer, 0, read); 
     } 
     baos.flush(); 
     return Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT); 
    } catch (Exception e) { 
     Log.d("Error", e.toString()); 
    } 
    return null; 
} 
0

下面的代碼轉換成圖像以base64字符串:

public String getBase64EncodedImage(String imageURL) throws IOException { 
    java.net.URL url = new java.net.URL(imageURL); 
    InputStream is = url.openStream(); 
    byte[] bytes = org.apache.commons.io.IOUtils.toByteArray(is); 
    return Base64.encodeBase64String(bytes); 
} 

附:以上代碼使用commons-io作爲依賴項。

相關問題