# Base Encoding Decoding Examples

## 1. Encoding Image to an BASE64 format so that it can be shared to web application via Backend Rest API.

### Step 1: Download any image and place it in a folder accessible to java project

<figure><img src="https://3632859567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPd6ktrA5pPLsZJktj2fm%2Fuploads%2Fgit-blob-c1bf4bb0d5a3002ec1de78c243c6be8061046b00%2Farrows.jpg?alt=media" alt="" width="375"><figcaption><p>arrows.jpg</p></figcaption></figure>

<figure><img src="https://3632859567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPd6ktrA5pPLsZJktj2fm%2Fuploads%2Fgit-blob-86c6c02998b11ba60a55a2119ba9bb35f1399e67%2Fimage.png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

### Step 2: Run the below Java Code to generate Base64 content of the above image

```java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;

public class ImageToBase64Conversion {

    public static void main(String[] args) throws IOException {
        // Path to our image file
        String imageBasePath = "/Users/pranayp/Documents/Project/Personal/sample-java-project/src/main/resources/image/";
        String arrowImage = imageBasePath + "arrows.jpg";

        try {
            // Convert image to Base64
            String base64Image = encodeImageToBase64(arrowImage);

            // Print the Base64 string
            System.out.println("--------------------");
            System.out.println(base64Image);
            System.out.println("--------------------");

        } catch (IOException e) {
            System.err.println("Exception occurred: " + e.getMessage());
        }
    }

    /**
     * Encodes the image at the given path to a Base64 string.
     *
     * @param imagePath the path of the image file to encode
     * @return the Base64-encoded string representation of the image
     * @throws IOException if an I/O error occurs during file reading
     */
    public static String encodeImageToBase64(String imagePath) throws IOException {
        // Create a File object from the given image path
        File file = new File(imagePath);

        // Allocate a byte array large enough to hold the entire file content
        byte[] imageBytes = new byte[(int) file.length()];

        // Use try-with-resources to ensure the FileInputStream is closed automatically
        try (FileInputStream fileInputStream = new FileInputStream(file)) {
            // Read the file content into the byte array
            fileInputStream.read(imageBytes);
        }

        // Encode the byte array to a Base64 string and return it
        return Base64.getEncoder().encodeToString(imageBytes);
    }
}

```

**Output**

{% file src="<https://3632859567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPd6ktrA5pPLsZJktj2fm%2Fuploads%2Fgit-blob-f90f4cc1e72b6d486bb8f38fde738779351be35d%2FOutput.txt?alt=media>" %}

### Step 3: Copy the Base64 content to any online website which converts base64 to image in order to verify the encoded image

Sample Website: <https://base64.guru/converter/decode/image>

<figure><img src="https://3632859567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPd6ktrA5pPLsZJktj2fm%2Fuploads%2Fgit-blob-31b68aebc029d60626fed63cf631097eb59b773d%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/spring/concepts-set-3/encoding-or-decoding/base-encoding-decoding-examples.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
