Java tutorial
/* * Copyright 2007-2107 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.ymate.platform.mvc.web.support; import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import net.ymate.platform.mvc.web.IUploadFileWrapper; import net.ymate.platform.mvc.web.IWebMultipartHandler; import net.ymate.platform.mvc.web.WebMVC; import net.ymate.platform.mvc.web.annotation.FileUpload; import net.ymate.platform.mvc.web.impl.DefaultWebMultipartHandler; import org.apache.commons.lang.StringUtils; /** * <p> * MultipartRequestWrapper * </p> * <p> * "multipart/form-data"? * </p> * * @author (suninformation@163.com) * @version 0.0.0 * <table style="border:1px solid gray;"> * <tr> * <th width="100px">?</th><th width="100px"></th><th * width="100px"></th><th width="100px"></th> * </tr> * <!-- Table ?? --> * <tr> * <td>0.0.0</td> * <td></td> * <td></td> * <td>2011-8-5?10:19:47</td> * </tr> * </table> */ public class MultipartRequestWrapper extends HttpServletRequestWrapper { private IWebMultipartHandler __handler; /** * * @param request * @param upload * @throws Exception */ public MultipartRequestWrapper(HttpServletRequest request, FileUpload upload) throws Exception { super(request); __handler = WebMVC.getConfig().getMultipartHandlerClassImpl(); if (__handler == null) { __handler = new DefaultWebMultipartHandler(); } __handler.doHandler(upload); } /* (non-Javadoc) * @see javax.servlet.ServletRequestWrapper#getParameter(java.lang.String) */ public String getParameter(String name) { String _returnStr = super.getParameter(name); if (StringUtils.isBlank(_returnStr)) { String[] params = __handler.getFieldMap().get(name); _returnStr = (params == null ? null : params[0]); } return _returnStr; } /* (non-Javadoc) * @see javax.servlet.ServletRequestWrapper#getParameterValues(java.lang.String) */ public String[] getParameterValues(String name) { String[] _returnStr = super.getParameterValues(name); if (_returnStr == null || _returnStr.length == 0) { _returnStr = __handler.getFieldMap().get(name); } return _returnStr; } /* (non-Javadoc) * @see javax.servlet.ServletRequestWrapper#getParameterMap() */ public Map<String, String[]> getParameterMap() { @SuppressWarnings("unchecked") Map<String, String[]> _returnMap = new HashMap<String, String[]>(super.getParameterMap()); _returnMap.putAll(__handler.getFieldMap()); return Collections.unmodifiableMap(_returnMap); } /* (non-Javadoc) * @see javax.servlet.ServletRequestWrapper#getParameterNames() */ public Enumeration<String> getParameterNames() { Enumeration<String> names = new Enumeration<String>() { private Iterator<String> it = getParameterMap().keySet().iterator(); public boolean hasMoreElements() { return it.hasNext(); } public String nextElement() { return it.next(); } }; return names; } /** * @param name ?? * @return ? */ public IUploadFileWrapper getFile(String name) { IUploadFileWrapper[] files = __handler.getFileMap().get(name); return files == null ? null : files[0]; } /** * @param name ?? * @return ? */ public IUploadFileWrapper[] getFiles(String name) { return __handler.getFileMap().get(name); } }