Search in sources :

Example 1 with FastUploading

use of org.nutz.mvc.upload.FastUploading in project nutz by nutzam.

the class WhaleFilter method handleUpload.

@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);
    }
}
Also used : UploadException(org.nutz.mvc.upload.UploadException) ArrayList(java.util.ArrayList) ServletException(javax.servlet.ServletException) FastUploading(org.nutz.mvc.upload.FastUploading) Entry(java.util.Map.Entry) TempFile(org.nutz.mvc.upload.TempFile) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper)

Aggregations

ArrayList (java.util.ArrayList)1 Entry (java.util.Map.Entry)1 ServletException (javax.servlet.ServletException)1 HttpServletRequestWrapper (javax.servlet.http.HttpServletRequestWrapper)1 FastUploading (org.nutz.mvc.upload.FastUploading)1 TempFile (org.nutz.mvc.upload.TempFile)1 UploadException (org.nutz.mvc.upload.UploadException)1