The dynamic model of human papillomavirus(HPV) with both sexual and nonsexual transmission is established, the expressions for HPV transmission routes of heterosexual females, heterosexual males and men who have sex with men(MSM) in the model are given, the transmission threshold of the model is derived, the dynamic behavior is analyzed in heterosexual population and MSM. Parameters estimation and numerical simulations are carried out by actual data, which from Xingning City, Guangdong Province, China, and the sensitivity analysis of threshold is carried out. The results find that nonsexual contact has a significant impact on the spread of HPV. Finally, optimal control of the model is studied. The results show that among the different control costs, the lower the control cost of the individual burden, the more the number of infected individuals that can be afforded, which leads to the lower the number of the overall people infected. Therefore, in the long run, low-cost control is the optimal control. It is also found that for the prevention of HPV, it is not only necessary to actively vaccinate, reduce the number of sexual partners, but also pay attention to nonsexual transmission.