h:commandButton is not firing action if the h:form has enctype=“multipart/form-data”

后端 未结 2 1208
误落风尘
误落风尘 2021-01-15 12:54

JSF: 1.2

Server: Weblogic

I am trying to implement multiple file upload. As I need to give support for IE7 so I cannot use

2条回答
  •  隐瞒了意图╮
    2021-01-15 13:32

    As @BalusC instructed:

    import java.io.IOException;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Enumeration;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    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;
    import javax.servlet.http.HttpServletRequestWrapper;
    
    import org.apache.commons.fileupload.FileItem;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;
    
    
    public class UploadFilter implements Filter {
        private FilterConfig filterConfig = null;
    
        public void init(FilterConfig filterConfig) throws ServletException {
            this.filterConfig = filterConfig;
        }
    
        public void destroy() {
            this.filterConfig = null;
        }
    
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            if (!(request instanceof HttpServletRequest)) {
                chain.doFilter(request, response);
                return;
            }
    
            HttpServletRequest httpRequest = (HttpServletRequest)request;
    
            boolean isMultipartContent = ServletFileUpload.isMultipartContent(httpRequest);
    
            if (!isMultipartContent) {
                chain.doFilter(request, response);
                return;
            }        
    
            try {
                DiskFileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload upload = new ServletFileUpload(factory);
                upload.setHeaderEncoding("UTF-8");
                upload.setSizeMax(-1);
    
                List items = upload.parseRequest(httpRequest);
                final Map parameterMap = new HashMap();
    
                for (FileItem item : items) {
                    if (item.isFormField()) {
                        processFormField(item, parameterMap);
                    } else {
                        processFileField(item, httpRequest);
                    }
                }
    
                chain.doFilter(new HttpServletRequestWrapper(httpRequest) {
    
                        public Map getParameterMap() {
                            return parameterMap;
                        }
    
                        public String[] getParameterValues(String name) {
                            return (String[])parameterMap.get(name);
                        }
    
                        public String getParameter(String name) {
                            String[] params = getParameterValues(name);
                            if (params == null) {
                                return null;
                            }
                            return params[0];
                        }
    
                        public Enumeration getParameterNames() {
                            return Collections.enumeration(parameterMap.keySet());
                        }
                    }, response);
            } catch (Exception ex) {
                ServletException servletException = new ServletException();
                servletException.initCause(ex);
                throw servletException;
            }
        }
    
        private void processFormField(FileItem formField, Map parameterMap) {
            String name = formField.getFieldName();
            String value = formField.getString();
            String[] values = parameterMap.get(name);
    
            if (values == null) {
                parameterMap.put(name, new String[] { value });
            } else {
                int length = values.length;
                String[] newValues = new String[length + 1];
                System.arraycopy(values, 0, newValues, 0, length);
                newValues[length] = value;
                parameterMap.put(name, newValues);
            }
        }
    
        private void processFileField(FileItem fileField, HttpServletRequest request) {
            if (request.getAttribute(fileField.getFieldName()) == null) {
                List fileFields = new ArrayList(0);
                fileFields.add(fileField);
                request.setAttribute(fileField.getFieldName(), fileFields);
            } else {
                List fileFields = (List)request.getAttribute(fileField.getFieldName());
                fileFields.add(fileField);
            }
        }
    }
    

    Since I will have multiple input file component; so I have used List to store all the file fields in the method processFileField and then have set it to the request.

提交回复
热议问题