Java XML Base64 Encode Decode createBase64EncodedStringFromURL(URL url)

Here you can find the source of createBase64EncodedStringFromURL(URL url)

Description

create Base Encoded String From URL

License

Apache License

Declaration

public static String createBase64EncodedStringFromURL(URL url) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.xml.bind.DatatypeConverter;

public class Main {
    public static String createBase64EncodedStringFromURL(URL url) throws IOException {
        final InputStream inputStream = url.openStream();
        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        final byte[] buffer = new byte[4096];

        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }//from w  w  w. jav  a 2 s .  com

        outputStream.close();
        inputStream.close();

        final byte[] bytes = outputStream.toByteArray();
        return "data:image/png;base64," + DatatypeConverter.printBase64Binary(bytes);
    }
}

Related

  1. base64ToByte(String data)
  2. byteArrayToBase64String(byte[] data)
  3. BytesToBase64String(final byte[] value)
  4. byteToBase64(byte[] data)
  5. compressToBase64(byte[] message)
  6. decode(String base64)
  7. decodeBase64(File file)
  8. decodeBase64(String b64data)
  9. decodeBase64(String base64)