当上传图片时出现 HTTP ERROR 302 错误, 这是服务端需要登录验证,而客户端uploadify 并没有把 cookie 中的 sessionId 发送给服务器 ,如果有拦截或过滤的话,请求就会被重置,这样就会出现302错误了。

 

网上有一种解决方法就是在请求后面加上 sessionId (http://fanshuyao.iteye.com/admin/blogs/1751684),但好像也不怎么行。现在通过实现HttpSessionListener的sessionCreated和sessionDestroyed 能很好解决问题。

 

步骤如下:


1、建立自己的SessionContext

Java代码 复制代码 收藏代码
  1. /**
  2. * session管理,解决uploadify session丢失问题
  3. * @author lqy
  4. * @date 2013-1-29
  5. */
  6. public class MySessionContext {
  7. private static MySessionContext instance;
  8. private HashMap<String, HttpSession> mymap;
  9.  
  10. private MySessionContext() {
  11. mymap = new HashMap<String, HttpSession>();
  12. }
  13.  
  14. public static MySessionContext getInstance() {
  15. if (instance == null) {
  16. instance = new MySessionContext();
  17. }
  18. return instance;
  19. }
  20.  
  21. public synchronized void addSession(HttpSession session) {
  22. if (session != null) {
  23. mymap.put(session.getId(), session);
  24. }
  25. }
  26.  
  27. public synchronized void delSession(HttpSession session) {
  28. if (session != null) {
  29. mymap.remove(session.getId());
  30. }
  31. }
  32.  
  33. public synchronized HttpSession getSession(String session_id) {
  34. if (session_id == null)
  35. return null;
  36. return (HttpSession) mymap.get(session_id);
  37. }
  38.  
  39. }

/**
 * session管理,解决uploadify session丢失问题
 * @author lqy
 * @date 2013-1-29
 */
public class MySessionContext {
	private static MySessionContext instance;
	private HashMap<String, HttpSession> mymap;

	private MySessionContext() {
		mymap = new HashMap<String, HttpSession>();
	}

	public static MySessionContext getInstance() {
		if (instance == null) {
			instance = new MySessionContext();
		}
		return instance;
	}

	public synchronized void addSession(HttpSession session) {
		if (session != null) {
			mymap.put(session.getId(), session);
		}
	}

	public synchronized void delSession(HttpSession session) {
		if (session != null) {
			mymap.remove(session.getId());
		}
	}

	public synchronized HttpSession getSession(String session_id) {
		if (session_id == null)
			return null;
		return (HttpSession) mymap.get(session_id);
	}

}


2、新增一个Session监听类

Java代码 复制代码 收藏代码
  1. /**
  2. * Session监听器
  3. * @author lqy
  4. * @date 2013-1-29
  5. */
  6. public class SessionListener implements HttpSessionListener {
  7.  
  8. public static Map<String,HttpSession> userMap = new HashMap<String,HttpSession>();
  9.  
  10. private MySessionContext myc = MySessionContext.getInstance();
  11.  
  12. public void sessionCreated(HttpSessionEvent httpSessionEvent) {
  13. myc.addSession(httpSessionEvent.getSession());
  14. }
  15.  
  16. public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
  17. HttpSession session = httpSessionEvent.getSession();
  18. myc.delSession(session);
  19. }
  20.  
  21. }  

/**
 * Session监听器
 * @author lqy
 * @date 2013-1-29
 */
public class SessionListener implements HttpSessionListener {

	public static Map<String,HttpSession> userMap = new HashMap<String,HttpSession>();
	
	private MySessionContext myc = MySessionContext.getInstance();

	public void sessionCreated(HttpSessionEvent httpSessionEvent) {
		myc.addSession(httpSessionEvent.getSession());
	}

	public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
		HttpSession session = httpSessionEvent.getSession();
		myc.delSession(session);
	}

}