Generally, articles have text content and images. I want to implement an image service (or server) to process the image logic separately, which is separated from the article processing logic. So I came up with a way to try it.
Background:
1. If the article is processed in the web App, it is called web1. The image should be stored in a folder of the app. For example, the path should be http: // localhost: 8080/web1/img/1.jpg, there should be an image in the img folder of this website, but it does not actually exist.
2. We implement the separation of image logic and create a new web app called web2, which processes all image services in web2. The path of an image is http: // localhost: 8080/web2/1.jpg. The image is stored here.
Note the differences and functions: web1 is the logic of our article processing (maybe your main website), and web2 is the image service.
Ideas:
1. Write a filter. Implement forwarding when a path, such as http: // localhost: 8080/web1/img/1.jpg. There should be an image in the img folder of this website, but it does not actually exist. Forward all image requests from img/* to img.
Code:
= "ImageFilter", urlPatterns = "/img/*" ImageFilter HttpServletRequest req = req. getContextPath () + "/img/" = requestURI. substring (requestURI. indexOf (contextPath) + "str1:" + "getContextPath:" + "getRequestURI:" + "getRequestURL:" + "getPathInfo:" + (imageName! = &&! "" = Request. getServletContext (). getContext ("/web2" = "/xxx.jpg" = init (FilterConfig fConfig)View Code
2. Prepare images and process specific images under the IMG (process cache and convert Images Based on business needs ).
Problem to be solved: Cross-origin access problem
Under normal circumstances, cross-origin access is not allowed. Cross-origin access to context. xml needs to be configured. Method:
Attribute crossContext = "true" <context crossContext = "true">View Code
Demo source code download
Password 3f63 http://yunpan.cn/QN3XnTDxVIJtV
Refer:
Http://blog.csdn.net/qfs_v/article/details/2551762
Http://mn960mn.blog.163.com/blog/static/1141030842011020112410281/
Http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html