Objective: To analyze the species, density, distribution, and variation of flies in Shanghai, China, to carry out risk assessment, prediction, and early warning of fly-borne diseases in a timely manner, so as to provide scientific reference for fly control. Methods: Using the cage trap method, fly traps were placed in farmers' markets, residential areas, external environment of restaurants, and large green belts. Fly surveillance data were collected every ten days from March to November in 2016-2021 in Shanghai. The data were processed and analyzed using Excel 2019 and SPSS 20.0. Results: From 2016 to 2021, 18 052 flies were caught with an average density of 1.52 flies/cage. The identified fly species belonged to 5 families, 9 genera, and 14 species. The dominant species were Muscina stabulans, Boettcherisca peregrina, Chrysomya megacephala, Lucilia sericata, and Musca domestica. M. stabulans showed the highest density of 0.28 flies/cage, followed by B. peregrina (0.24 flies/cage). In large green belts, the density of B. peregrina was higher than that of M. stabulans, and in all other habitats, the density of M. stabulans was the highest. There was a significant difference in density between B. peregrina and M. stabulans in the farmers' market (t=-2.674, P=0.023). The proportion of Mu. domestica decreased year by year, and the proportion of M. stabulans increased. The fly density was the highest in 2018 (1.82 flies/cage) and the lowest in 2016 (1.04 flies/cage). Fly density peaked from June to August, with the monthly average densities of 2.65, 2.49, and 2.28 flies/cage, respectively. In 2016-2021, the fly density in different habitats was in the order of farmers' markets > large green belts > residential areas > external environment of restaurants. Conclusions: The fly species are diverse in Shanghai. M. stabulans has the highest density. Fly density peaks from June to August, and farmer's market is a key place for targeted fly control. Fly density rose again after decline in 2019, suggesting that comprehensive control of flies should be continuously strengthened.