源码(三) - RequestContext和ServletRequestContext

xiaoxiao2021-02-28  61

一.概述

1.一个servlet上传文件,构造参数为HttpServletRequest,可以通过此参数获取请求信息 获取上传文件的编码类型:如UTF-8获取上传文件的内容类型:servlet上传文件以"multipart/"开始获取上传文件的大小获取上传文件的输入流

二.RequestContext

抽象访问文件上传所需的请求信息。 // 1.2.1 package org.apache.commons.fileupload; import java.io.InputStream; import java.io.IOException; /** * 抽象访问文件上传所需的请求信息。 * 对于可以由FileUpload处理的每种类型的请求(如servlet和portlet),都应该实现这种互操作。 * * @since FileUpload 1.1 */ public interface RequestContext { /** * 返回请求的字符编码。 * @return The character encoding for the request. */ String getCharacterEncoding(); /** * 获取请求的内容类型。 * @return The content type of the request. */ String getContentType(); /** * 获取请求的内容长度。 * @return The content length of the request. */ int getContentLength(); /** * 获取请求的输入流。 * @return The input stream for the request. * @throws IOException if a problem occurs. */ InputStream getInputStream() throws IOException; }

三.ServletRequestContext

// 1.2.1 package org.apache.commons.fileupload.servlet; import java.io.InputStream; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import org.apache.commons.fileupload.RequestContext; /** * Provides access to the request information needed for a request made to an HTTP servlet. * * 提供对HTTP servlet请求所需的请求信息的访问。 * * @since FileUpload 1.1 * */ public class ServletRequestContext implements RequestContext { /** * The request for which the context is being provided. * 提供上下文的请求。 */ private HttpServletRequest request; /** * Construct a context for this request. * @param request The request to which this context applies. */ public ServletRequestContext(HttpServletRequest request) { this.request = request; } public String getCharacterEncoding() { return request.getCharacterEncoding(); } public String getContentType() { return request.getContentType(); } public int getContentLength() { return request.getContentLength(); } /** * 从请求对象中获取上传文件的输入流 * */ public InputStream getInputStream() throws IOException { return request.getInputStream(); } public String toString() { return "ContentLength=" + this.getContentLength() + ", ContentType=" + this.getContentType(); } }

3.一个实例截图

转载请注明原文地址: https://www.6miu.com/read-79163.html

最新回复(0)