(Java) Base64 Encode a File
Java to Base64 encode the contents of a file.
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[])
{
// Get the contents of a file into a base64 encoded string:
CkFileAccess fac = new CkFileAccess();
String strBase64 = fac.readBinaryToEncoded("c:/data/something.pdf","base64");
if (fac.get_LastMethodSuccess() != true) {
System.out.println(fac.lastErrorText());
return;
}
// Now write the string to a file:
boolean success = fac.WriteEntireTextFile("c:/data/something_pdf_base64.txt",strBase64,"us-ascii",false);
if (success != true) {
System.out.println(fac.lastErrorText());
return;
}
System.out.println("Success!");
}
}
|