The most efficient way would be to keep the image path in app configuration and use different configurations for development and production. The dev version will do localhost (or whatever) and the prod version will point to your CDN.
If configuration is not an option, Jason Hunter's Java Servlet Programming has an example of search/replacing outgoing html with a regex. You can use and adaptation of this to replace your image URLs.
If your app server is fronted by a load balancer or Apache you could also do the replacement there. It has the benefit of not polluting your app with CDN logic. But the drawback is that it is harder to version control, etc.