Struts2 FCKeditor 文件上传解决

Struts2下FCKeditor图片上传不成功,相信大家都遇到过,网上也有不少解决方法!

说出来其实很简单,就是在struts2的Filter之前配一个过滤器,拦截"/fckeditor/editor/filemanager/connectors/*"再做forward转发,

过滤器配置
package com.common.service.filter;
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class FckeditorUploadFilter implements Filter {
	private String DispatcherUrl;
	public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)
	          throws IOException,ServletException{
		HttpServletRequest httprequest = (HttpServletRequest) request;
		if(httprequest.getParameter("Command")!=null&&
				httprequest.getParameter("Command").equals("FileUpload")){
			httprequest.getRequestDispatcher(DispatcherUrl).forward(request, response);
		}else{
			chain.doFilter(request, response);
		}
	}
	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init(FilterConfig config){
		// Put your code here
		DispatcherUrl=config.getInitParameter("DispatcherUrl");
	}
	public void destroy(){
		
	}

}


web.xml配置
	<servlet>
		<servlet-name>ConnectorServlet</servlet-name>
		<servlet-class>
			net.fckeditor.connector.ConnectorServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>ConnectorServlet</servlet-name>
		<url-pattern>
			/fckeditor/editor/filemanager/connectors/*
		</url-pattern>
	</servlet-mapping>

	<filter>
		<filter-name>FckeditorUploadFilter</filter-name>
		<filter-class>
			com.common.service.filter.FckeditorUploadFilter
		</filter-class>
		<init-param>
			<param-name>DispatcherUrl</param-name>
			<param-value>/fckeditor/editor/filemanager/connectors/</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>FckeditorUploadFilter</filter-name>
		<url-pattern>
			/fckeditor/editor/filemanager/connectors/*
		</url-pattern>
	</filter-mapping>

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
		</filter-class>

	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

你可能感兴趣的:(apache,Web,servlet,struts,fckeditor)