From 09cefc2b8970eea56d81b1a886d9bb769a48daf3 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Wed, 21 Oct 2015 12:34:14 +0000 Subject: [PATCH] MultipartFormEntity#getContent implementation Contributed by Slikey git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.5.x@1709814 13f79535-47bb-0310-9956-ffa450edef68 --- .../http/entity/mime/MultipartFormEntity.java | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/httpmime/src/main/java/org/apache/http/entity/mime/MultipartFormEntity.java b/httpmime/src/main/java/org/apache/http/entity/mime/MultipartFormEntity.java index 467ca8e08..cabb824ae 100644 --- a/httpmime/src/main/java/org/apache/http/entity/mime/MultipartFormEntity.java +++ b/httpmime/src/main/java/org/apache/http/entity/mime/MultipartFormEntity.java @@ -27,16 +27,19 @@ package org.apache.http.entity.mime; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.http.ContentTooLongException; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.entity.ContentType; import org.apache.http.message.BasicHeader; import org.apache.http.protocol.HTTP; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - @SuppressWarnings("deprecation") class MultipartFormEntity implements HttpEntity { @@ -94,8 +97,15 @@ class MultipartFormEntity implements HttpEntity { @Override public InputStream getContent() throws IOException { - throw new UnsupportedOperationException( - "Multipart form entity does not implement #getContent()"); + if (this.contentLength < 0) { + throw new ContentTooLongException("Content length is unknown"); + } else if (this.contentLength > 25 * 1024) { + throw new ContentTooLongException("Content length is too long: " + this.contentLength); + } + final ByteArrayOutputStream outstream = new ByteArrayOutputStream(); + writeTo(outstream); + outstream.flush(); + return new ByteArrayInputStream(outstream.toByteArray()); } @Override