|
Struts example source code file (MultiPartRequestWrapper.java)
The Struts MultiPartRequestWrapper.java source code/* * $Id: MultiPartRequestWrapper.java 651946 2008-04-27 13:41:38Z apetrelli $ * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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 org.apache.struts2.dispatcher.multipart; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.Vector; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.dispatcher.StrutsRequestWrapper; import com.opensymphony.xwork2.util.logging.Logger; import com.opensymphony.xwork2.util.logging.LoggerFactory; /** * Parse a multipart request and provide a wrapper around the request. The parsing implementation used * depends on the <tt>struts.multipart.parser setting. It should be set to a class which * extends {@link org.apache.struts2.dispatcher.multipart.MultiPartRequest}. * <p/> * The <tt>struts.multipart.parser property should be set to jakarta for the * Jakarta implementation, <tt>pell for the Pell implementation and cos for the Jason Hunter * implementation. * <p/> * The files are uploaded when the object is instantiated. If there are any errors they are logged using * {@link #addError(String)}. An action handling a multipart form should first check {@link #hasErrors()} * before doing any other processing. * <p/> * An alternate implementation, PellMultiPartRequest, is provided as a plugin. * */ public class MultiPartRequestWrapper extends StrutsRequestWrapper { protected static final Logger LOG = LoggerFactory.getLogger(MultiPartRequestWrapper.class); Collection<String> errors; MultiPartRequest multi; /** * Process file downloads and log any errors. * * @param request Our HttpServletRequest object * @param saveDir Target directory for any files that we save * @param multiPartRequest Our MultiPartRequest object */ public MultiPartRequestWrapper(MultiPartRequest multiPartRequest, HttpServletRequest request, String saveDir) { super(request); multi = multiPartRequest; try { multi.parse(request, saveDir); for (Object o : multi.getErrors()) { String error = (String) o; addError(error); } } catch (IOException e) { addError("Cannot parse request: "+e.toString()); } } /** * Get an enumeration of the parameter names for uploaded files * * @return enumeration of parameter names for uploaded files */ public Enumeration<String> getFileParameterNames() { if (multi == null) { return null; } return multi.getFileParameterNames(); } /** * Get an array of content encoding for the specified input field name or <tt>null if * no content type was specified. * * @param name input field name * @return an array of content encoding for the specified input field name */ public String[] getContentTypes(String name) { if (multi == null) { return null; } return multi.getContentType(name); } /** * Get a {@link java.io.File[]} for the given input field name. * * @param fieldName input field name * @return a File[] object for files associated with the specified input field name */ public File[] getFiles(String fieldName) { if (multi == null) { return null; } return multi.getFile(fieldName); } /** * Get a String array of the file names for uploaded files * * @param fieldName Field to check for file names. * @return a String[] of file names for uploaded files */ public String[] getFileNames(String fieldName) { if (multi == null) { return null; } return multi.getFileNames(fieldName); } /** * Get the filename(s) of the file(s) uploaded for the given input field name. * Returns <tt>null if the file is not found. * * @param fieldName input field name * @return the filename(s) of the file(s) uploaded for the given input field name or * <tt>null if name not found. */ public String[] getFileSystemNames(String fieldName) { if (multi == null) { return null; } return multi.getFilesystemName(fieldName); } /** * @see javax.servlet.http.HttpServletRequest#getParameter(String) */ public String getParameter(String name) { return ((multi == null) || (multi.getParameter(name) == null)) ? super.getParameter(name) : multi.getParameter(name); } /** * @see javax.servlet.http.HttpServletRequest#getParameterMap() */ public Map getParameterMap() { Map<String, String[]> map = new HashMap Other Struts examples (source code examples)Here is a short list of links related to this Struts MultiPartRequestWrapper.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.