File upload using robot class in selenium webdriver

File Upload using Robot Class in Selenium

import java.awt.AWTException;

import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
 
import com.sun.glass.events.KeyEvent;
 
public class FileUploadClass {
 
 
public static void main(String[] args) throws Exception {
 
System.setProperty("webdriver.chrome.driver", "chrome bowser exe file path");
 
     WebDriver driver= new ChromeDriver();
 
 
     Thread.sleep(5000);
 
     driver.manage().window().maximize();
 
     Robot robot = new Robot();
   
     driver.findElement(By.id("imagesrc")).click();
 
     robot.setAutoDelay(2000);
 
     StringSelection stringselection = new StringSelection("upload file path");
 
     Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringselection, null);
     
     robot.setAutoDelay(3000);
     
     robot.keyPress(KeyEvent.VK_CONTROL);
 
     robot.keyPress(KeyEvent.VK_V);
 
     robot.keyRelease(KeyEvent.VK_CONTROL);
 
     robot.keyRelease(KeyEvent.VK_V);
 
     robot.setAutoDelay(3000);
 
     robot.keyPress(KeyEvent.VK_ENTER);
 
     robot.keyRelease(KeyEvent.VK_ENTER);
}