multipartparamutils.java
来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 277 行
JAVA
277 行
/** * $RCSfile: MultipartParamUtils.java,v $ * $Revision: 1.3 $ * $Date: 2002/06/10 14:45:14 $ * * 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.jivesoftware.forum.util;import java.io.*;import java.util.*;import javax.activation.*;import javax.mail.*;import javax.mail.internet.*;import javax.servlet.*;/** * 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 if (part.getContent() instanceof String) { parameters.put(name + "Data", new ByteArrayInputStream( ((String)part.getContent()).getBytes())); } else { parameters.put(name + "Data", part.getContent()); } // 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 ignored) {} } // 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 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) { e.printStackTrace(); } } 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 + =
减小字号Ctrl + -
显示快捷键?