Please check out our FAQ section on Japan, specifically the sections on whether or not Japan was isolationist and why did Japan succeed.
u/brindlekin and u/PantsTime team up to answer this question here, to get you started. There’s loads of answers to this question scattered across the sub if you want to perform a quick search.
I think my response from last week may be applied to your question.
Since "global empires" -though I question whether to call Japan as such- were based out of the west at that point, it follows that as long as you can use their capital, buy their technology and trace their path, one can catch up. As to how they got from "revere the Emperor and expel the barbarians" during the Boshin War to then, we need to look at who was actually doing the fighting. Specifically, how the interests of the West was divided into two by region.
Due to the proximity of Kyoto versus Edo, the western samurais rose up to champion their emperor, anchored by Choshu, Satsuma and Tosa. The shogun tried to reach a peaceful settlement with the rebels, but having failed, he began hiring and buying from the French. The rebels responded by importing ships from the Brits.
Even from this early point, you can see how the clans' interests are not aligned with their own slogan with which they riled up the mainland samurais. The treaty ports of Kagoshima, Nagasaki and Shimonoseki were what allowed Choshu and Satsuma to gain enough power to face an entire nation. And it would be the Satsuma clan's particularly strong ties to the Brits that would allow them an advantage at sea with which they would win the war, as well as the conflict with the Ezo Republic.
The Satsuma Rebellion was also not about fighting Westernization as is often portrayed. Takamori had been pushing for another invasion of Korea to point the anger of the disgruntled and now unemployed samurai outwards, but having failed and facing the dissolution of the class in its entirety, felt honor-bound to lead his clan into rebellion. It was a war of the classes and timing, of when they were going to use the battle-hardened populace.
The rebellion caused Japan to leave the gold standard and go into a recession. In response, untapped production capacity was spent on military build-up, fueled by foreign debt. Qing ended up paying for everything and more when they sued for peace. On the other hand, the Russo-Japanese War was seen as a great failure domestically, not having been able to draw out a notable sum out of the Russians. American expats in Japan were harassed due to "their" role in interfering in the negotiations.
Russia interfered in the negotiations after the First Sino-Japanese War and was attacked. The USA arbitrated the Treaty of Portsmouth and Wilheim II tried to sow dissension between the two parties by starting up the whole "Yellow Peril" campaign. And when Japan did finally go to war with the USA, the Korean War followed soon after, allowing Japan to use their military infrastructure to supply the Americans. The reputation of quality then carried over to their automobiles.