You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2013/06/20 21:06:33 UTC
svn commit: r1495134 - in /httpcomponents/httpclient/trunk/httpmime/src:
main/java/org/apache/http/entity/mime/ test/java/org/apache/http/entity/mime/
Author: olegk
Date: Thu Jun 20 19:06:33 2013
New Revision: 1495134
URL: http://svn.apache.org/r1495134
Log:
Removed code duplication between deprecated HttpMultipart and AbstractMultipartForm; made AbstractMultipartForm and its super classes package private
Added:
httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/AbstractMultipartForm.java (contents, props changed)
- copied, changed from r1495022, httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpMultipartForm.java
Removed:
httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpMultipartForm.java
Modified:
httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpBrowserCompatibleMultipart.java
httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpMultipart.java
httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpMultipartFactory.java
httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpStrictMultipart.java
httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/MultipartEntity.java
httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java
Copied: httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/AbstractMultipartForm.java (from r1495022, httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpMultipartForm.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/AbstractMultipartForm.java?p2=httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/AbstractMultipartForm.java&p1=httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpMultipartForm.java&r1=1495022&r2=1495134&rev=1495134&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpMultipartForm.java (original)
+++ httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/AbstractMultipartForm.java Thu Jun 20 19:06:33 2013
@@ -47,7 +47,7 @@ import org.apache.http.util.ByteArrayBuf
*
* @since 4.3
*/
-public abstract class HttpMultipartForm {
+abstract class AbstractMultipartForm {
private static ByteArrayBuffer encode(
final Charset charset, final String string) {
@@ -106,10 +106,9 @@ public abstract class HttpMultipartForm
* @param subType mime subtype - must not be {@code null}
* @param charset the character set to use. May be {@code null}, in which case {@link MIME#DEFAULT_CHARSET} - i.e. US-ASCII - is used.
* @param boundary to use - must not be {@code null}
- * @param mode the mode to use
* @throws IllegalArgumentException if charset is null or boundary is null
*/
- public HttpMultipartForm(final String subType, final Charset charset, final String boundary) {
+ public AbstractMultipartForm(final String subType, final Charset charset, final String boundary) {
super();
Args.notNull(subType, "Multipart subtype");
Args.notNull(boundary, "Multipart boundary");
@@ -119,7 +118,7 @@ public abstract class HttpMultipartForm
this.parts = new ArrayList<FormBodyPart>();
}
- public HttpMultipartForm(final String subType, final String boundary) {
+ public AbstractMultipartForm(final String subType, final String boundary) {
this(subType, null, boundary);
}
@@ -182,8 +181,6 @@ public abstract class HttpMultipartForm
* Writes out the content in the multipart/form encoding. This method
* produces slightly different formatting depending on its compatibility
* mode.
- *
- * @see #getMode()
*/
public void writeTo(final OutputStream out) throws IOException {
doWriteTo(out, true);
Propchange: httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/AbstractMultipartForm.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/AbstractMultipartForm.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/AbstractMultipartForm.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpBrowserCompatibleMultipart.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpBrowserCompatibleMultipart.java?rev=1495134&r1=1495133&r2=1495134&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpBrowserCompatibleMultipart.java (original)
+++ httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpBrowserCompatibleMultipart.java Thu Jun 20 19:06:33 2013
@@ -27,36 +27,29 @@
package org.apache.http.entity.mime;
-import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
-import java.nio.ByteBuffer;
-import java.nio.CharBuffer;
import java.nio.charset.Charset;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.http.entity.mime.content.ContentBody;
-import org.apache.http.util.Args;
-import org.apache.http.util.ByteArrayBuffer;
/**
- * HttpBrowserCompatibleMultipart represents a collection of MIME multipart encoded content bodies. This class is
- * emulates browser compatibility, e.g. IE 5 or earlier.
+ * HttpBrowserCompatibleMultipart represents a collection of MIME multipart encoded
+ * content bodies. This class is emulates browser compatibility, e.g. IE 5 or earlier.
*
* @since 4.3
*/
-public class HttpBrowserCompatibleMultipart extends HttpMultipartForm {
+class HttpBrowserCompatibleMultipart extends AbstractMultipartForm {
/**
* Creates an instance with the specified settings.
*
* @param subType mime subtype - must not be {@code null}
- * @param charset the character set to use. May be {@code null}, in which case {@link MIME#DEFAULT_CHARSET} - i.e. US-ASCII - is used.
+ * @param charset the character set to use. May be {@code null},
+ * in which case {@link MIME#DEFAULT_CHARSET} - i.e. US-ASCII - is used.
* @param boundary to use - must not be {@code null}
* @throws IllegalArgumentException if charset is null or boundary is null
*/
- public HttpBrowserCompatibleMultipart(final String subType, final Charset charset, final String boundary) {
+ public HttpBrowserCompatibleMultipart(
+ final String subType, final Charset charset, final String boundary) {
super(subType, charset, boundary);
}
@@ -69,8 +62,8 @@ public class HttpBrowserCompatibleMultip
*/
@Override
protected void formatMultipartHeader(
- final FormBodyPart part,
- final OutputStream out) throws IOException {
+ final FormBodyPart part,
+ final OutputStream out) throws IOException {
// For browser-compatible, only write Content-Disposition
// Use content charset
final Header header = part.getHeader();
Modified: httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpMultipart.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpMultipart.java?rev=1495134&r1=1495133&r2=1495134&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpMultipart.java (original)
+++ httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpMultipart.java Thu Jun 20 19:06:33 2013
@@ -27,18 +27,9 @@
package org.apache.http.entity.mime;
-import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
-import java.nio.ByteBuffer;
-import java.nio.CharBuffer;
import java.nio.charset.Charset;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.http.entity.mime.content.ContentBody;
-import org.apache.http.util.Args;
-import org.apache.http.util.ByteArrayBuffer;
/**
* HttpMultipart represents a collection of MIME multipart encoded content bodies. This class is
@@ -46,60 +37,11 @@ import org.apache.http.util.ByteArrayBuf
* the browser compatible modes.
*
* @since 4.0
+ *
+ * @deprecated (4.3) Use {@link MultipartEntityBuilder}.
*/
@Deprecated
-public class HttpMultipart {
-
- private static ByteArrayBuffer encode(
- final Charset charset, final String string) {
- final ByteBuffer encoded = charset.encode(CharBuffer.wrap(string));
- final ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining());
- bab.append(encoded.array(), encoded.position(), encoded.remaining());
- return bab;
- }
-
- private static void writeBytes(
- final ByteArrayBuffer b, final OutputStream out) throws IOException {
- out.write(b.buffer(), 0, b.length());
- }
-
- private static void writeBytes(
- final String s, final Charset charset, final OutputStream out) throws IOException {
- final ByteArrayBuffer b = encode(charset, s);
- writeBytes(b, out);
- }
-
- private static void writeBytes(
- final String s, final OutputStream out) throws IOException {
- final ByteArrayBuffer b = encode(MIME.DEFAULT_CHARSET, s);
- writeBytes(b, out);
- }
-
- private static void writeField(
- final MinimalField field, final OutputStream out) throws IOException {
- writeBytes(field.getName(), out);
- writeBytes(FIELD_SEP, out);
- writeBytes(field.getBody(), out);
- writeBytes(CR_LF, out);
- }
-
- private static void writeField(
- final MinimalField field, final Charset charset, final OutputStream out) throws IOException {
- writeBytes(field.getName(), charset, out);
- writeBytes(FIELD_SEP, out);
- writeBytes(field.getBody(), charset, out);
- writeBytes(CR_LF, out);
- }
-
- private static final ByteArrayBuffer FIELD_SEP = encode(MIME.DEFAULT_CHARSET, ": ");
- private static final ByteArrayBuffer CR_LF = encode(MIME.DEFAULT_CHARSET, "\r\n");
- private static final ByteArrayBuffer TWO_DASHES = encode(MIME.DEFAULT_CHARSET, "--");
-
-
- private final String subType;
- private final Charset charset;
- private final String boundary;
- private final List<FormBodyPart> parts;
+public class HttpMultipart extends AbstractMultipartForm {
private final HttpMultipartMode mode;
@@ -107,19 +49,16 @@ public class HttpMultipart {
* Creates an instance with the specified settings.
*
* @param subType mime subtype - must not be {@code null}
- * @param charset the character set to use. May be {@code null}, in which case {@link MIME#DEFAULT_CHARSET} - i.e. US-ASCII - is used.
+ * @param charset the character set to use. May be {@code null},
+ * in which case {@link MIME#DEFAULT_CHARSET} - i.e. US-ASCII - is used.
* @param boundary to use - must not be {@code null}
* @param mode the mode to use
* @throws IllegalArgumentException if charset is null or boundary is null
*/
- public HttpMultipart(final String subType, final Charset charset, final String boundary, final HttpMultipartMode mode) {
- super();
- Args.notNull(subType, "Multipart subtype");
- Args.notNull(boundary, "Multipart boundary");
- this.subType = subType;
- this.charset = charset != null ? charset : MIME.DEFAULT_CHARSET;
- this.boundary = boundary;
- this.parts = new ArrayList<FormBodyPart>();
+ public HttpMultipart(
+ final String subType, final Charset charset, final String boundary,
+ final HttpMultipartMode mode) {
+ super(subType, charset, boundary);
this.mode = mode;
}
@@ -128,7 +67,8 @@ public class HttpMultipart {
* Mode is set to {@link HttpMultipartMode#STRICT}
*
* @param subType mime subtype - must not be {@code null}
- * @param charset the character set to use. May be {@code null}, in which case {@link MIME#DEFAULT_CHARSET} - i.e. US-ASCII - is used.
+ * @param charset the character set to use. May be {@code null},
+ * in which case {@link MIME#DEFAULT_CHARSET} - i.e. US-ASCII - is used.
* @param boundary to use - must not be {@code null}
* @throws IllegalArgumentException if charset is null or boundary is null
*/
@@ -140,120 +80,30 @@ public class HttpMultipart {
this(subType, null, boundary);
}
- public String getSubType() {
- return this.subType;
- }
-
- public Charset getCharset() {
- return this.charset;
- }
-
public HttpMultipartMode getMode() {
return this.mode;
}
- public List<FormBodyPart> getBodyParts() {
- return this.parts;
- }
-
- public void addBodyPart(final FormBodyPart part) {
- if (part == null) {
- return;
- }
- this.parts.add(part);
- }
-
- public String getBoundary() {
- return this.boundary;
- }
-
- private void doWriteTo(
- final HttpMultipartMode mode,
- final OutputStream out,
- final boolean writeContent) throws IOException {
-
- final ByteArrayBuffer boundary = encode(this.charset, getBoundary());
- for (final FormBodyPart part: this.parts) {
- writeBytes(TWO_DASHES, out);
- writeBytes(boundary, out);
- writeBytes(CR_LF, out);
-
- final Header header = part.getHeader();
-
- switch (mode) {
- case STRICT:
- for (final MinimalField field: header) {
- writeField(field, out);
- }
- break;
+ @Override
+ protected void formatMultipartHeader(
+ final FormBodyPart part, final OutputStream out) throws IOException {
+ final Header header = part.getHeader();
+ switch (this.mode) {
case BROWSER_COMPATIBLE:
- // Only write Content-Disposition
+ // For browser-compatible, only write Content-Disposition
// Use content charset
- final MinimalField cd = part.getHeader().getField(MIME.CONTENT_DISPOSITION);
+ final MinimalField cd = header.getField(MIME.CONTENT_DISPOSITION);
writeField(cd, this.charset, out);
final String filename = part.getBody().getFilename();
if (filename != null) {
- final MinimalField ct = part.getHeader().getField(MIME.CONTENT_TYPE);
+ final MinimalField ct = header.getField(MIME.CONTENT_TYPE);
writeField(ct, this.charset, out);
}
break;
- }
- writeBytes(CR_LF, out);
-
- if (writeContent) {
- part.getBody().writeTo(out);
- }
- writeBytes(CR_LF, out);
- }
- writeBytes(TWO_DASHES, out);
- writeBytes(boundary, out);
- writeBytes(TWO_DASHES, out);
- writeBytes(CR_LF, out);
- }
-
- /**
- * Writes out the content in the multipart/form encoding. This method
- * produces slightly different formatting depending on its compatibility
- * mode.
- *
- * @see #getMode()
- */
- public void writeTo(final OutputStream out) throws IOException {
- doWriteTo(this.mode, out, true);
- }
-
- /**
- * Determines the total length of the multipart content (content length of
- * individual parts plus that of extra elements required to delimit the parts
- * from one another). If any of the @{link BodyPart}s contained in this object
- * is of a streaming entity of unknown length the total length is also unknown.
- * <p/>
- * This method buffers only a small amount of data in order to determine the
- * total length of the entire entity. The content of individual parts is not
- * buffered.
- *
- * @return total length of the multipart entity if known, <code>-1</code>
- * otherwise.
- */
- public long getTotalLength() {
- long contentLen = 0;
- for (final FormBodyPart part: this.parts) {
- final ContentBody body = part.getBody();
- final long len = body.getContentLength();
- if (len >= 0) {
- contentLen += len;
- } else {
- return -1;
- }
- }
- final ByteArrayOutputStream out = new ByteArrayOutputStream();
- try {
- doWriteTo(this.mode, out, false);
- final byte[] extra = out.toByteArray();
- return contentLen + extra.length;
- } catch (final IOException ex) {
- // Should never happen
- return -1;
+ default:
+ for (final MinimalField field: header) {
+ writeField(field, out);
+ }
}
}
Modified: httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpMultipartFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpMultipartFactory.java?rev=1495134&r1=1495133&r2=1495134&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpMultipartFactory.java (original)
+++ httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpMultipartFactory.java Thu Jun 20 19:06:33 2013
@@ -39,8 +39,8 @@ public class HttpMultipartFactory {
private HttpMultipartFactory() {
}
- /** Create an appropriate HttpMultipartForm instance */
- public static HttpMultipartForm getInstance(
+ /** Create an appropriate AbstractMultipartForm instance */
+ public static AbstractMultipartForm getInstance(
final String subType, final Charset charset, final String boundary,
final HttpMultipartMode mode) {
// If needed, this can be replaced with a registry in time
Modified: httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpStrictMultipart.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpStrictMultipart.java?rev=1495134&r1=1495133&r2=1495134&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpStrictMultipart.java (original)
+++ httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/HttpStrictMultipart.java Thu Jun 20 19:06:33 2013
@@ -27,32 +27,25 @@
package org.apache.http.entity.mime;
-import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
-import java.nio.ByteBuffer;
-import java.nio.CharBuffer;
import java.nio.charset.Charset;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.http.entity.mime.content.ContentBody;
-import org.apache.http.util.Args;
-import org.apache.http.util.ByteArrayBuffer;
/**
- * HttpStrictMultipart represents a collection of MIME multipart encoded content bodies, implementing the
- * strict (RFC 822, RFC 2045, RFC 2046 compliant) interpretation of the spec.
+ * HttpStrictMultipart represents a collection of MIME multipart encoded content bodies,
+ * implementing the strict (RFC 822, RFC 2045, RFC 2046 compliant) interpretation
+ * of the spec.
*
* @since 4.3
*/
-public class HttpStrictMultipart extends HttpMultipartForm {
+class HttpStrictMultipart extends AbstractMultipartForm {
/**
* Creates an instance with the specified settings.
*
* @param subType mime subtype - must not be {@code null}
- * @param charset the character set to use. May be {@code null}, in which case {@link MIME#DEFAULT_CHARSET} - i.e. US-ASCII - is used.
+ * @param charset the character set to use. May be {@code null},
+ * in which case {@link MIME#DEFAULT_CHARSET} - i.e. US-ASCII - is used.
* @param boundary to use - must not be {@code null}
* @throws IllegalArgumentException if charset is null or boundary is null
*/
Modified: httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/MultipartEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/MultipartEntity.java?rev=1495134&r1=1495133&r2=1495134&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/MultipartEntity.java (original)
+++ httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/MultipartEntity.java Thu Jun 20 19:06:33 2013
@@ -53,7 +53,7 @@ public class MultipartEntity implements
"-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
.toCharArray();
- private final HttpMultipartForm multipart;
+ private final AbstractMultipartForm multipart;
private final Header contentType;
// @GuardedBy("dirty") // we always read dirty before accessing length
@@ -84,7 +84,7 @@ public class MultipartEntity implements
* @param charset the character set to use, may be {@code null}, in which case {@link MIME#DEFAULT_CHARSET} - i.e. US-ASCII - is used.
*/
public MultipartEntity(
- final HttpMultipartForm multipart,
+ final AbstractMultipartForm multipart,
final String boundary,
final Charset charset) {
super();
@@ -136,7 +136,7 @@ public class MultipartEntity implements
/**
* @since 4.3
*/
- protected HttpMultipartForm getMultipart() {
+ protected AbstractMultipartForm getMultipart() {
return multipart;
}
Modified: httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java?rev=1495134&r1=1495133&r2=1495134&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java (original)
+++ httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java Thu Jun 20 19:06:33 2013
@@ -46,7 +46,7 @@ public class TestMultipartForm {
@Test
public void testMultipartFormStringParts() throws Exception {
- final HttpMultipartForm multipart = new HttpStrictMultipart("form-data", "foo");
+ final AbstractMultipartForm multipart = new HttpStrictMultipart("form-data", "foo");
final FormBodyPart p1 = new FormBodyPart(
"field1",
new StringBody("this stuff", ContentType.DEFAULT_TEXT));
@@ -102,7 +102,7 @@ public class TestMultipartForm {
writer.close();
}
- final HttpMultipartForm multipart = new HttpStrictMultipart("form-data", "foo");
+ final AbstractMultipartForm multipart = new HttpStrictMultipart("form-data", "foo");
final FormBodyPart p1 = new FormBodyPart(
"field1",
new FileBody(tmpfile));
@@ -152,7 +152,7 @@ public class TestMultipartForm {
}
// Strict is no accident here, despite the test name - otherwise Transfer-Encoding is not produced.
- final HttpMultipartForm multipart = new HttpStrictMultipart("form-data", null, "foo");
+ final AbstractMultipartForm multipart = new HttpStrictMultipart("form-data", null, "foo");
final FormBodyPart p1 = new FormBodyPart(
"field1",
new FileBody(tmpfile));
@@ -234,7 +234,7 @@ public class TestMultipartForm {
writer.close();
}
- final HttpMultipartForm multipart = new HttpBrowserCompatibleMultipart("form-data", Charset.forName("UTF-8"), "foo");
+ final AbstractMultipartForm multipart = new HttpBrowserCompatibleMultipart("form-data", Charset.forName("UTF-8"), "foo");
final FormBodyPart p1 = new FormBodyPart(
"field1",
new InputStreamBody(new FileInputStream(tmpfile), s1 + ".tmp"));
@@ -275,7 +275,7 @@ public class TestMultipartForm {
final String s1 = constructString(SWISS_GERMAN_HELLO);
final String s2 = constructString(RUSSIAN_HELLO);
- final HttpMultipartForm multipart = new HttpStrictMultipart("form-data", "foo");
+ final AbstractMultipartForm multipart = new HttpStrictMultipart("form-data", "foo");
final FormBodyPart p1 = new FormBodyPart(
"field1",
new StringBody(s1, ContentType.create("text/plain", Charset.forName("ISO-8859-1"))));