I found the article "Why Japanese People AreComfortable With Nakedness" on this website. It is true that we have many chances to see naked Japanese men especially at festivals, and the festivals called "裸祭 Hadaka Matsuri" (naked festival)" were held all over Japan. But I don't know if this phenomenon is related to onsen, Shinto, or prostitution, as this article pointed out.
Off course Japanese men don't get be naked at just anytime, anywhere. We have a strict rule on when and where we can be naked. I'm living near Ikebukuro Station, which is one of the biggest terminal stations in Tokyo, and during a festival I see many naked Japanese men walking on the street just in front of Ikebukuro Station and foreigner people watch them with curiosity and take photos. But we never see naked men on an ordinary day.
In every culture there is a rule about the costume that participants of festivals should wear. Nakedness is the costume of the male participants of Hadaka Matsuri (naked festival) just like the female participants of Rio's Carnival. The article said, "While sexuality is not encouraged in most Western religions, Japan’s native Shinto religion is more open-minded," but the main religion in Brazil is Christianity.
It is common that things that aren't allowed in everyday are allowed in festival time. The participants of Hadaka Matsuri (naked festival) and Rio's Carnival turn to be naked, because they aren't allowed to be naked in everyday. Japanese folklore scholars call this phenomenon "ハレとケ (hare and ke)". "Hare" means festival time, and "ke" means everyday life.
We also turn to be naked in a public bath and onsen. It is not that we are allowed to be naked in the public bath but we SHOULD be naked in the public bath. Off course it is rude to watch other's naked bodies. In the hinterlands there are mixed bathing onsens, which aren't sexual at all. You can see just old naked men and women.