(Java) Get Contents of File as Base64
Demonstrates how to read the contents of a file and convert to a base64 string.
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
CkBinData bd = new CkBinData();
boolean success = bd.LoadFile("qa_data/jpg/starfish.jpg");
if (success == false) {
System.out.println("Failed to load file.");
return;
}
System.out.println(bd.getEncoded("base64"));
// If you want mult-line base64:
System.out.println("--");
System.out.println(bd.getEncoded("base64_mime"));
// If you want hex..
System.out.println("--");
System.out.println(bd.getEncoded("hex"));
// etc.
}
}
|