我想對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);
}
'FileInputStream imageInFile = new FileInputStream(is.toString());'這是錯誤的,請閱讀API文檔,確保您完全理解代碼中每一行的內容。 – Jesper
我知道這是錯誤的部分。我拿一個文件來做例子。但我試圖用URL和InputStream –