Skip to content

Commit

Permalink
fix: 抽取WhaleFilter的关于上传文件的代码
Browse files Browse the repository at this point in the history
  • Loading branch information
wendal committed Nov 28, 2016
1 parent ddc60f0 commit bdde20a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 28 deletions.
82 changes: 55 additions & 27 deletions src/org/nutz/mvc/WhaleFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
import java.io.InputStream;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import javax.servlet.Filter;
Expand All @@ -22,11 +26,14 @@
import javax.servlet.http.HttpServletResponse;

import org.nutz.filepool.UU32FilePool;
import org.nutz.lang.Each;
import org.nutz.lang.Lang;
import org.nutz.lang.Mirror;
import org.nutz.lang.util.NutMap;
import org.nutz.log.LogAdapter;
import org.nutz.log.Logs;
import org.nutz.mvc.upload.FastUploading;
import org.nutz.mvc.upload.TempFile;
import org.nutz.mvc.upload.UploadException;
import org.nutz.mvc.upload.UploadingContext;

Expand Down Expand Up @@ -102,7 +109,6 @@ public void init(InputStream ins) throws Exception {
}
}

@SuppressWarnings("unchecked")
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
Expand Down Expand Up @@ -143,32 +149,7 @@ public String getMethod() {
String contentType = req.getContentType();
if (contentType != null) {
if (uc != null && contentType.contains("multipart/form-data")) {
FastUploading fup = new FastUploading();
try {
final Map<String, Object> params = fup.parse(req, (UploadingContext) uc);
params.putAll(req.getParameterMap());
request = new HttpServletRequestWrapper(req) {
public String getParameter(String name) {
return (String) params.get(name);
}
@SuppressWarnings("rawtypes")
public Map getParameterMap() {
return params;
}
@SuppressWarnings("rawtypes")
public Enumeration getParameterNames() {
return Collections.enumeration(params.keySet());
}
public String[] getParameterValues(String name) {
if (params.containsKey(name))
return new String[]{(String) params.get(name)};
return null;
}
};
}
catch (UploadException e) {
throw new ServletException("upload fail", e);
}
request = handleUpload(req);
}
}
}
Expand All @@ -177,4 +158,51 @@ public String[] getParameterValues(String name) {
}

public void destroy() {}

@SuppressWarnings("unchecked")
public HttpServletRequest handleUpload(HttpServletRequest req) throws ServletException {
try {
FastUploading fup = new FastUploading();
final Map<String, Object> params = fup.parse(req, (UploadingContext) uc);
final List<TempFile> files = new ArrayList<TempFile>();
Iterator<Entry<String, Object>> it = params.entrySet().iterator();
while (it.hasNext()) {
Object obj = it.next().getValue();
final boolean[] re = new boolean[1];
Lang.each(obj, new Each<Object>() {
public void invoke(int index, Object ele, int length){
if (ele != null && ele instanceof TempFile) {
files.add((TempFile) ele);
re[0] = true;
}
}
});
if (re[0])
it.remove();
}
req.setAttribute("_files", files);
params.putAll(req.getParameterMap());
return new HttpServletRequestWrapper(req) {
public String getParameter(String name) {
return (String) params.get(name);
}
@SuppressWarnings("rawtypes")
public Map getParameterMap() {
return params;
}
@SuppressWarnings("rawtypes")
public Enumeration getParameterNames() {
return Collections.enumeration(params.keySet());
}
public String[] getParameterValues(String name) {
if (params.containsKey(name))
return new String[]{(String) params.get(name)};
return null;
}
};
}
catch (UploadException e) {
throw new ServletException("upload fail", e);
}
}
}
3 changes: 2 additions & 1 deletion src/org/nutz/mvc/view/AbstractPathView.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.nutz.mvc.view;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -102,7 +103,7 @@ public static Context createContext(HttpServletRequest req, Object obj) {
}
// 请求的参数表,需要兼容之前的p.参数, Fix issue 418
Map<String, String> p = new HashMap<String, String>();
for (Object o : req.getParameterMap().keySet()) {
for (Object o : Lang.enum2collection(req.getParameterNames(), new ArrayList<String>())) {
String key = (String) o;
String value = req.getParameter(key);
p.put(key, value);
Expand Down

0 comments on commit bdde20a

Please sign in to comment.