![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Java) Upload a File to an AI Provider (OpenAI, Google, Antropic, X)See more AI ExamplesUploads a file to an AI provider using the provider's File API and returns theid that can later be used to reference the file in an query. This currently works with ChatGPT, Gemini, Claude, and Grok.Note: This example requires Chilkat v11.4.0 or greater.
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[]) { boolean success = false; CkAi ai = new CkAi(); // The provider can be "openai", "google", "claude", "grok", or any AI that supports uploading files for later reference by ID. ai.put_Provider("openai"); // Use your provider's API key. ai.put_ApiKey("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 = ai.uploadFile(localFilePath,contentType); if (ai.get_LastMethodSuccess() == false) { System.out.println(ai.lastErrorText()); System.out.println("AI File Upload Failed."); } else { System.out.println("File ID: " + file_id); System.out.println("File uploaded."); } // Upload from the contents of a StringBuilder CkStringBuilder sb = new CkStringBuilder(); success = sb.LoadFile(localFilePath,"utf-8"); if (success == false) { System.out.println(sb.lastErrorText()); return; } file_id = ai.uploadFileSb(sb,filenameOnServer,contentType); if (ai.get_LastMethodSuccess() == false) { System.out.println(ai.lastErrorText()); System.out.println("AI File Upload Failed."); } else { System.out.println("File ID: " + file_id); System.out.println("File uploaded."); } // Upload from the contents of a BinData CkBinData bd = new CkBinData(); success = bd.LoadFile(localFilePath); if (success == false) { System.out.println(bd.lastErrorText()); return; } file_id = ai.uploadFileBd(bd,filenameOnServer,contentType); if (ai.get_LastMethodSuccess() == false) { System.out.println(ai.lastErrorText()); System.out.println("AI File Upload Failed."); } else { System.out.println("File ID: " + file_id); System.out.println("File uploaded."); } } } |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.