Example usage for org.apache.commons.net.util Base64 encodeBase64String

List of usage examples for org.apache.commons.net.util Base64 encodeBase64String

Introduction

In this page you can find the example usage for org.apache.commons.net.util Base64 encodeBase64String.

Prototype

public static String encodeBase64String(byte[] binaryData) 

Source Link

Document

Encodes binary data using the base64 algorithm into 76 character blocks separated by CRLF.

Usage

From source file:tds.itemrenderer.processing.ITSUrlGridTask.java

/**
 * Convert to base 64/*from  w w  w  . j  a v  a  2  s.  c  om*/
 * 
 * @param imageFilePath
 * @return
 */
private String getBase64(String imageFilePath) {
    // create data uri format
    String dataUriFormat = "data:image/%s;base64,";
    String imageExt = Path.getExtension(imageFilePath).replace(".", "");
    String dataUri = String.format(dataUriFormat, imageExt);

    // get base64 data
    byte[] imageBytes = null;
    try {
        imageBytes = FileUtils.readFileToByteArray(new File(imageFilePath));
    } catch (IOException e) {
        throw new ITSDocumentProcessingException(e);
    }
    String imageBase64 = Base64.encodeBase64String(imageBytes);

    // return data uri
    return dataUri + imageBase64;
}