⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multipartparamutils.java

📁 这个是网络上下载的一个struct框架的程序
💻 JAVA
字号:
/** * $RCSfile: MultipartParamUtils.java,v $ * $Revision: 1.5 $ * $Date: 2003/05/30 14:26:02 $ * * Copyright (C) 1999-2002 CoolServlets, Inc. All rights reserved. * * This software is the proprietary information of CoolServlets, Inc. * Use is subject to license terms. */package com.struts2.framework.util;import javax.activation.*;import javax.mail.*;import javax.mail.internet.*;import javax.servlet.ServletRequest;import java.io.*;import java.util.Enumeration;import java.util.HashMap;import java.util.Map;import java.util.Iterator;/** * This class retrieves web page parameters from a multipart/form-data encoded * HTML form and stores them as key/values pairs. It also stores any parameters * found in the ServletRequest object (non-multipart/form-data parameters). *  * @see ParamUtils */public class MultipartParamUtils {	private Map parameters;	/**	 * Initialize the class with the ServletRequest object and the maximum	 * allowable request size (in bytes).	 * 	 * @param request	 *            the ServletRequest object	 * @param maxRequestSize	 *            the maximum allowable incoming request size, in bytes.	 */	public MultipartParamUtils(ServletRequest request, int maxRequestSize) throws IOException {		// Initialize the internal map of parameter name/value pairs		this.parameters = new HashMap();		String contentType = request.getContentType();		// loop through the request and pull out any multipart text/plain		// values and store them as the parameters		if (contentType != null				&& contentType.startsWith("multipart/form-data")) {			try {				// Put the request's input stream into a Multipart object.				Multipart multipart = new MimeMultipart(new MemoryDataSource(						request.getInputStream(), contentType, maxRequestSize));				// For each part in the multipart object:				for (int i = 0; i < multipart.getCount(); i++) {					Part part = multipart.getBodyPart(i);					// For all headers in this part:					for (Enumeration enum = part.getAllHeaders(); enum							.hasMoreElements();) {						// The part header holds the parameter name						String paramName = ((Header) enum.nextElement())								.getValue();						// If the paramName contains "filename=" then this is						// a file, otherwise we'll treat is as a regular						// parameter.						if (paramName.indexOf("filename=\"") > -1) {							// We have to get both the name= and filename=							// fields.							int nStart = paramName.indexOf("name=\"")									+ "name=\"".length();							int nEnd = paramName.indexOf("\"", nStart);							int fnStart = paramName.indexOf("filename=\"")									+ "filename=\"".length();							int fnEnd = paramName.indexOf("\"", fnStart);							String name = paramName.substring(nStart, nEnd);							String filename = paramName.substring(fnStart,									fnEnd);							// Store the name/file name as one key/value							parameters.put(name, filename);							// Store the name/part content as another							parameters									.put(name + "Data", part.getInputStream());							// Store the name/part contenet type as another							parameters.put(name + "ContentType", part									.getContentType());							// Store the file size as another parameter							parameters.put(name + "Size", new Integer(part									.getSize()));						} else {							// This parameter is not a file, just a normal							// text/plain param.							int nStart = paramName.indexOf("name=\"");							nStart += "name=\"".length();							int nEnd = paramName.lastIndexOf("\"");							if (nStart > -1 && nEnd > -1) {								paramName = paramName.substring(nStart, nEnd);								if (paramName != null) {									Object value = part.getContent();									if (value != null) {										parameters.put(paramName, value												.toString().trim());									}								}							}						}					}				}			} catch (MessagingException e) {			}		}		// Try to add any non-multipart/form-data parameters found in the		// request object to the internal parameter map.		for (Enumeration enum = request.getParameterNames(); enum				.hasMoreElements();) {			String name = (String) enum.nextElement();			parameters.put(name, request.getParameter(name));		}	}	public Iterator getParameterNames() {		return parameters.keySet().iterator();	}	public String getParameter(String name, boolean emptyStringOK) {		if (name == null) {			return null;		}		String value = null;		if (parameters.containsKey(name)) {			value = (parameters.get(name)).toString();			if ("".equals(value) && !emptyStringOK) {				value = null;			}		}		return value;	}	public String getParameter(String name) {		return getParameter(name, false);	}	public int getIntParameter(String name, int defaultValue) {		int value = defaultValue;		String param = getParameter(name);		if (param != null) {			try {				value = Integer.parseInt(param);			} catch (NumberFormatException ignored) {			}		}		return value;	}	public long getLongParameter(String name, long defaultValue) {		long value = defaultValue;		String param = getParameter(name);		if (param != null) {			try {				value = Long.parseLong(param);			} catch (NumberFormatException ignored) {			}		}		return value;	}	public boolean getBooleanParameter(String name) {		String param = getParameter(name);		if (param != null && ("true".equals(param) || "on".equals(param))) {			return true;		}		return false;	}	// Methods for extra multipart parameter attributes	/**	 * Returns an InputStream of the given parameter's data. Will return null if	 * the name is invalid or the parameter is not found.	 */	public InputStream getParameterData(String name) {		if (name == null) {			return null;		}		Object obj = parameters.get(name + "Data");		if (obj != null) {			return (InputStream) obj;		} else {			return null;		}	}	/**	 * Returns the size of the given parameter in bytes or 0 if the parameter is	 * not found.	 */	public int getParameterSize(String name) {		if (name == null) {			return 0;		}		Object obj = parameters.get(name + "Size");		if (obj != null) {			return ((Integer) obj).intValue();		} else {			return 0;		}	}	/**	 * Returns the content type of the named parameter. Returns null if the name	 * is invalid or "text/plain" if the type is unknown.	 */	public String getParameterContentType(String name) {		if (name == null) {			return null;		}		Object obj = parameters.get(name + "ContentType");		if (obj != null) {			return (String) obj;		} else {			return "text/plain";		}	}}class MemoryDataSource implements DataSource {	private String contentType;	private ByteArrayOutputStream buf;	public MemoryDataSource(InputStream in, String contentType,			int maxRequestSize) {		this.contentType = contentType;		buf = new ByteArrayOutputStream();		try {			int data = -1;			// Keep count of how much data we've written in. If it exceeds			// maxRequestSize, stop reading data			int size = 0;			while ((data = in.read()) > -1) {				buf.write(data);				size++;				if (size > maxRequestSize) {					break;				}			}		} catch (Exception e) {		}	}	public String getContentType() {		return contentType;	}	public String getName() {		return "";	}	public InputStream getInputStream() throws IOException {		return new ByteArrayInputStream(buf.toByteArray());	}	public OutputStream getOutputStream() throws IOException {		throw new UnsupportedOperationException();	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -