Sample code for 30+ languages & platforms
Java Requires Chilkat v11.4.0+

Upload a File to an AI Provider (OpenAI, Google, Antropic, X)

See more AI Examples

Uploads a file to an AI provider using the provider's File API and returns the id that can later be used to reference the file in an query. This currently works with ChatGPT, Gemini, Claude, and Grok.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Ai;
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        Ai ai = new Ai();

        // The provider can be "openai", "google", "claude", "grok", or any AI that supports uploading files for later reference by ID.
        ai.setProvider("openai");
        // Use your provider's API key.
        ai.setApiKey("MY_API_KEY");

        // We can upload directly from a file in the filesystem, or from a Chilkat StringBuilder, or from a Chilkat BinData.

        // Some AI providers require a content-type.
        // Also, some AI providers are picky about what content-type's are accepted.
        // Check the AI provider's documentation.
        // "application/json" is generally always acceptable.
        // "text/plain" can be used as a fallback for any text file.
        String contentType = "application/json";
        String localFilePath = "qa_data/hamlet.json";
        String filenameOnServer = "hamlet.json";

        // Upload directly from a file.
        String file_id = null;
        try {
            file_id = ai.uploadFile(localFilePath, contentType);
            System.out.println("File ID: " + file_id);
            System.out.println("File uploaded.");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            System.out.println("AI File Upload Failed.");
        }

        // Upload from the contents of a StringBuilder
        StringBuilder sb = new StringBuilder();
        try {
            sb.loadFile(localFilePath, "utf-8");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        try {
            file_id = ai.uploadFileSb(sb, filenameOnServer, contentType);
            System.out.println("File ID: " + file_id);
            System.out.println("File uploaded.");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            System.out.println("AI File Upload Failed.");
        }

        // Upload from the contents of a BinData
        BinData bd = new BinData();
        try {
            bd.loadFile(localFilePath);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        try {
            file_id = ai.uploadFileBd(bd, filenameOnServer, contentType);
            System.out.println("File ID: " + file_id);
            System.out.println("File uploaded.");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            System.out.println("AI File Upload Failed.");
        }
    }
}