The suspect was arrested in the tourist haven of Palma de Mallorca and his hate-filled personal diary and other papers revealed plans to attack the University of the Balearic Islands, they said.
Identified only by the initials JMMS, he was detained on Wednesday just as he received more than 140 kilograms of explosives that he had bought online, police said in a statement.
"In his personal diary and other documents seized at his home, the detainee showed his hatred of society, especially university students, and his decision to strategically place shrapnel-filled pipe bombs in the university grounds, admitting to a possible suicide in the carrying out of the massacre," the statement said.
Police launched an investigation five months ago after the suspect's blog showed sympathy for Eric Harris and Dylan Klebold, who carried out the Columbine school massacre in Colorado on April 20, 1999, shooting dead 12 students and a teacher.
"In his blog he included sections in which he spoke about having identical tastes in music, a liking of weapons, clothing and even the social isolation which led him to be marginalised in school," the statement said.