Java
Java
Upload an Image File to an AI Provider (OpenAI, Google, Antropic, X)
See more AI Examples
Uploads an image 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 and Gemini, but not other AI's. Most AI's currently don't have the ability to reference pre-uploaded image data in conversations.
Chilkat Java Downloads
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();
// Can currently be "openai" or "google" because these are the only AI's that can reference pre-loaded image data by file 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 BinData.
String contentType = "image/jpeg";
String localFilePath = "qa_data/jpg/starfish.jpg";
String filenameOnServer = "starfish.jpg";
// 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 Chilkat 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.");
}
}
}