package org.nutz.mvc.upload;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;

/**
 * 关于上传的一些帮助函数
 * 
 * @author zozoh(zozohtnt@gmail.com)
 */
public abstract class Uploads {

	/**
	 * @param req
	 *            请求对象
	 * @return 当前会话的上传进度对象，如果没有上传，则返回 null
	 */
	public static UploadInfo getInfo(HttpServletRequest req) {
		return (UploadInfo) req.getSession().getAttribute(UploadInfo.class.getName());
	}

	/**
	 * @param req
	 *            请求对象
	 * @return 本次上传的进度对象
	 */
	public static UploadInfo createInfo(HttpServletRequest req) {
		UploadInfo info = new UploadInfo();
		HttpSession sess = req.getSession();
		if (null != sess) {
			sess.setAttribute(UploadInfo.SESSION_NAME, info);
		}
		info.sum = req.getContentLength();
		return info;
	}

	/**
	 * 根据请求对象创建参数 MAP， 同时根据 QueryString，为 MAP 设置初始值
	 * 
	 * @param req
	 *            请求对象
	 * @return 参数 MAP
	 */
	public static NutMap createParamsMap(HttpServletRequest req) {
		NutMap params = new NutMap();
		// parse query strings
		String qs = req.getQueryString();
		if (null != qs) {
			String[] pairs = Strings.splitIgnoreBlank(qs, "&");
			for (String pair : pairs) {
				String[] pp = pair.split("=");
				if (pp.length > 1)
					params.put(pp[0], pp[1]);
				else
					params.put(pp[0], null);
			}
		}
		return params;
	}

}

@@ -21,24 +21,78 @@
 .upload;%0D%0A%0D%0A
+import java.util.Map;%0D%0Aimport java.util.Map.Entry;%0D%0A%0D%0A
 import javax
@@ -172,39 +172,8 @@
 %0D%0A%0D%0A
-import org.nutz.lang.Strings;%0D%0A
 impo
@@ -1022,32 +1022,65 @@
 n %E5%8F%82%E6%95%B0 MAP%0D%0A%09 */%0D%0A
+%09@SuppressWarnings(%22unchecked%22)%0D%0A
 %09public static N
@@ -1193,188 +1193,212 @@
 %0D%0A%09%09
+Map%3C
 String
- qs = req.getQueryString();%0D%0A%09%09if (null != qs) %7B%0D%0A%09%09%09String%5B%5D pairs = Strings.splitIgnoreBlank(qs, %22&%22);%0D%0A%09%09%09for (String pair : pairs) %7B%0D%0A%09%09%09%09String%5B%5D pp = pair.split(%22=%22);
+, String %5B%5D%3E paramsZ = req.getParameterMap();%0D%0A%09%09if (null != paramsZ && paramsZ.size() %3E0  )%7B%0D%0A%09%09%09for (Entry%3CString, String %5B%5D%3E ppp : paramsZ.entrySet()) %7B%0D%0A%09%09%09%09if (ppp.getValue() != null ) %7B
 %0D%0A
+%09
 %09%09%09%09
@@ -1403,16 +1403,28 @@
 %09%09if (pp
+p.getValue()
 .length 
@@ -1429,12 +1429,13 @@
 h %3E 
-1
+0
 )%0D%0A
+%09
 %09%09%09%09
@@ -1452,22 +1452,42 @@
 t(pp
-%5B0%5D, pp%5B1
+p.getKey(), ppp.getValue()%5B0
 %5D);%0D%0A
+%09
 %09%09%09%09
@@ -1492,16 +1492,17 @@
 %09%09else%0D%0A
+%09
 %09%09%09%09%09par
@@ -1511,19 +1511,74 @@
 s.put(pp
-%5B0%5D
+p.getKey(), %22%22);%0D%0A%09%09%09%09%7D else%0D%0A%09%09%09%09%09params.put(ppp.getKey()
 , null);

Fix req的参数如果包含&的话, 值会被截断.