Java tutorial
/* * Copyright 2009-2011 Telkku.com * * 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 attila.core; import java.io.*; import java.util.*; import javax.servlet.http.*; import org.apache.commons.fileupload.*; import org.apache.commons.fileupload.disk.*; import org.apache.commons.fileupload.servlet.*; import attila.core.util.*; /** * Wrapper for HttpServletRequest that handles requests of type * POST with content type "multipart/form-data". * @author Eric Andrews * @author Kai Kousa */ public class MultipartRequest extends HttpServletRequestWrapper { private Map<String, List<String>> textParameters = null; private Map<String, FileItem> binaryParameters = null; public MultipartRequest(HttpServletRequest request) { super(request); } @Override public String getParameter(String name) { List<String> list = getTextParameters().get(name); if (list == null) { return null; } else { return list.get(0); } } public FileItem getFileParameter(String name) { return getBinaryParameters().get(name); } @SuppressWarnings("unchecked") @Override public Map getParameterMap() { Map<String, String[]> map = new HashMap<String, String[]>(); for (String name : getTextParameters().keySet()) { map.put(name, (String[]) getTextParameters().get(name).toArray(new String[1])); } return Collections.unmodifiableMap(map); } public Map<String, FileItem> getFileParameterMap() { return getBinaryParameters(); } @Override public Enumeration<String> getParameterNames() { return new IteratorEnumeration<String>(getTextParameters().keySet().iterator()); } public Enumeration<String> getFileParameterNames() { return new IteratorEnumeration<String>(getBinaryParameters().keySet().iterator()); } @Override public String[] getParameterValues(String name) { List<String> list = getTextParameters().get(name); if (list == null) { return null; } else { return list.toArray(new String[1]); } } @SuppressWarnings("unchecked") private void readParametersFromRequest(HttpServletRequest request) throws FileUploadException, UnsupportedEncodingException { textParameters = new HashMap<String, List<String>>(); binaryParameters = new HashMap<String, FileItem>(); ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory()); List<FileItem> items = upload.parseRequest(request); for (FileItem item : items) { if (item.isFormField()) { readTextParameter(item); } else { readBinaryParameter(item); } } } private void readTextParameter(FileItem item) throws UnsupportedEncodingException { byte[] bytes = item.getString().getBytes("ISO-8859-1"); String name = item.getFieldName(); String value = new String(bytes, getCharacterEncoding()); addTextParameter(name, value); } private void readBinaryParameter(FileItem item) { getBinaryParameters().put(item.getFieldName(), item); } @SuppressWarnings("unchecked") private void addTextParameter(String name, String value) { if (getTextParameters().containsKey(name)) { getTextParameters().get(name).add(value); } else { List list = new ArrayList<String>(); list.add(value); getTextParameters().put(name, list); } } private Map<String, List<String>> getTextParameters() { if (textParameters == null) { try { readParametersFromRequest((HttpServletRequest) getRequest()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileUploadException e) { e.printStackTrace(); } } return textParameters; } private Map<String, FileItem> getBinaryParameters() { if (binaryParameters == null) { try { readParametersFromRequest((HttpServletRequest) getRequest()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileUploadException e) { e.printStackTrace(); } } return binaryParameters; } }