JohnDoe4136
JohnDoe4136

Reputation: 539

Encode a file in Base64 format - Blackberry/J2ME

I am trying to upload a file to a http server, but first I need to encode this to a base64 format. How do I do that? I have open a fileconnection but i dont know how to encode the file.

Upvotes: 3

Views: 3267

Answers (3)

Mike
Mike

Reputation: 8545

If you're doing this in Java you can use the BASE64Encoder and write to a new encoded file:

import sun.misc.BASE64Encoder;

     public static void main(String[] args) throws Exception {

        File inputFile = new File(yourUnencodedFile);
        File outputFile = new File(yourEncodedFile);

        BASE64Encoder encoder = new BASE64Encoder();
        encoder.encode(
            new FileInputStream(inputFile),
            new FileOutputStream(outputFile)
        );

and then just use the encoded output file

Upvotes: -3

bharath
bharath

Reputation: 14453

See this java-me Base64 class for encoding with base64 format.

For more examples check koders or this Java and ME blog post: Base64 encode-decode in JavaMe.

Upvotes: 3

Marc Novakowski
Marc Novakowski

Reputation: 45398

Since BlackBerry is a J2ME environment, you can't use regular J2SE clases such as sun.misc.BASE64Encoder - but there is a native Base64OutputStream class which should serve the same purpose. See the javadocs for more info.

Upvotes: 3

Related Questions