In the application, the instance of FireFox was created by passing the DesiredCapabilities as follows
driver = new FirefoxDriver(capabilities);
Based on the suggestions by others, I did my changes as:
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/octet-stream");
driver = new FirefoxDriver(firefoxProfile);
This served the purpose but unfortunately, my other automation tests started failing. And the reason was, I have removed the capabilities which were being passed earlier.
There is an alternate way, through which we can set the profile itself in the desired Capabilities.
So the new working code looks like
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
// add more capabilities as per your need.
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/octet-stream");
// set the firefox profile as a capability
capabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
driver = new FirefoxDriver(capabilities);
Hope this code helps you!