Other things being equal, the AIs see the weakest empires as resources to be harvested. Having a decent military rating is indeed the most common way to avoid the problems you mentioned. There are few more aspects worth mentioning, I believe.
First, the AIs generally do not begin serious threats and wars until the colonization phase is ending. That is, AIs tend to expand first, and only start those other behaviors when their expansion has been halted. Thus, one can often predict when that behavior shift will begin.
Second, AIs do not break alliances. Thus, if you could get a full alliance with an AI, then that AI will not attack or threaten you. However, such alliances are tough to get and take time you may not have.
Third, AIs already in multiple wars tend not to start new ones with the human player unless you are really, really weak, and sometimes not even then.
The beauty of that last one above is that you can sometimes bribe all the AIs into so many mutual wars that they leave you alone. Just be sure to re-bribe them quickly if they sign a peace treaty. Generally, it costs less to bribe the stronger one to start the war, but the weaker one may declare the war for technology that you have that the stronger one already has (and so you cannot bribe them with it). Diplomacy ratings help here, as to the things that make your trades look better.
My rule of thumb is that every AI that has a higher military rating than I have should always be in two wars.