最近,遇到了这个问题,使用FckEditro上传图片时,出现了 下标越界异常。根据分析,得出结论,原来是struts2的拦截器把Fck的上传Servlet拦截了,经过了struts2对HttpServletRequest的包装,FCK已经无法正确的拿到正版的HttpServletRequest。
搜索了一下,发现不少人遇到这个问题,而解决方案是修改web.xml中
<filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
,把其修改为:
<filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*.action</url-pattern> </filter-mapping>
这样的话,隐患很大,问题很大,而且在2.1中会报错。具体不多说,上正题。
解决思路:就是让这个拦截器不拦截Fck的Servlet。
步骤:
1.下载struts2.1的源代码,拿出StrutsPrepareAndExecuteFilter.java ,自己在项目新建一个包,把这个类放进去。
2.修改web.xml中原来的过滤器类路径,改为自己的这个。如下:
<filter> <filter-name>struts2</filter-name> <filter-class>com.hs.fck.StrutsPrepareAndExecuteFilter</filter-class> </filter>
3.修改StrutsPrepareAndExecuteFilter.java,只需修改doFilter方法即可。如下:底色为绿色是修改的代码。也就是判断一下这个请求的servlet路径,如果是FCK的,就不执行包装。
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; if(request.getServletPath().equals("/fckeditor/editor/filemanager/connectors")) { chain.doFilter(request, response); }else{ try { prepare.setEncodingAndLocale(request, response); prepare.createActionContext(request, response); prepare.assignDispatcherToThread(); if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) { chain.doFilter(request, response); } else { request = prepare.wrapRequest(request); ActionMapping mapping = prepare.findActionMapping(request, response, true); if (mapping == null) { boolean handled = execute.executeStaticResourceRequest(request, response); if (!handled) { chain.doFilter(request, response); } } else { execute.executeAction(request, response, mapping); } } } finally { prepare.cleanupRequest(request); } } }
如有错误,欢迎大家指正。