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 2009/09/03 23:15:17 UTC
svn commit: r811126 - in /httpcomponents/httpclient/trunk/httpmime/src:
main/java/org/apache/http/entity/mime/content/FileBody.java
test/java/org/apache/http/entity/mime/TestMultipartForm.java
Author: olegk
Date: Thu Sep 3 21:15:15 2009
New Revision: 811126
URL: http://svn.apache.org/viewvc?rev=811126&view=rev
Log:
HTTPCLIENT-871: Add FileBody constructor with explicit filename
Contributed by Gerald Turner <gturner at unzane.com>
Modified:
httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/content/FileBody.java
httpcomponents/httpclient/trunk/httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartForm.java
Modified: httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/content/FileBody.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/content/FileBody.java?rev=811126&r1=811125&r2=811126&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/content/FileBody.java (original)
+++ httpcomponents/httpclient/trunk/httpmime/src/main/java/org/apache/http/entity/mime/content/FileBody.java Thu Sep 3 21:15:15 2009
@@ -45,13 +45,39 @@
public class FileBody extends AbstractContentBody {
private final File file;
-
- public FileBody(final File file, final String mimeType) {
+ private final String filename;
+ private final String charset;
+
+ /**
+ * @since 4.1
+ */
+ public FileBody(final File file,
+ final String filename,
+ final String mimeType,
+ final String charset) {
super(mimeType);
if (file == null) {
throw new IllegalArgumentException("File may not be null");
}
this.file = file;
+ if (filename != null)
+ this.filename = filename;
+ else
+ this.filename = file.getName();
+ this.charset = charset;
+ }
+
+ /**
+ * @since 4.1
+ */
+ public FileBody(final File file,
+ final String mimeType,
+ final String charset) {
+ this(file, null, mimeType, charset);
+ }
+
+ public FileBody(final File file, final String mimeType) {
+ this(file, mimeType, null);
}
public FileBody(final File file) {
@@ -93,7 +119,7 @@
}
public String getCharset() {
- return null;
+ return charset;
}
public long getContentLength() {
@@ -101,7 +127,7 @@
}
public String getFilename() {
- return this.file.getName();
+ return filename;
}
public File getFile() {
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=811126&r1=811125&r2=811126&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 Sep 3 21:15:15 2009
@@ -240,7 +240,7 @@
} finally {
writer.close();
}
-
+
HttpMultipart multipart = new HttpMultipart("form-data");
multipart.setParent(message);
FormBodyPart p1 = new FormBodyPart(
@@ -248,33 +248,48 @@
new FileBody(tmpfile));
FormBodyPart p2 = new FormBodyPart(
"field2",
+ new FileBody(tmpfile, "test-file", "text/plain", "ANSI_X3.4-1968"));
+ FormBodyPart p3 = new FormBodyPart(
+ "field3",
new InputStreamBody(new FileInputStream(tmpfile), "file.tmp"));
-
+
multipart.addBodyPart(p1);
multipart.addBodyPart(p2);
-
- multipart.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
-
+ multipart.addBodyPart(p3);
+
+ multipart.setMode(HttpMultipartMode.STRICT);
+
ByteArrayOutputStream out = new ByteArrayOutputStream();
multipart.writeTo(out);
out.close();
-
+
String expected =
"--foo\r\n" +
"Content-Disposition: form-data; name=\"field1\"; " +
"filename=\"" + tmpfile.getName() + "\"\r\n" +
+ "Content-Type: application/octet-stream\r\n" +
+ "Content-Transfer-Encoding: binary\r\n" +
"\r\n" +
"some random whatever\r\n" +
"--foo\r\n" +
"Content-Disposition: form-data; name=\"field2\"; " +
+ "filename=\"test-file\"\r\n" +
+ "Content-Type: text/plain; charset=ANSI_X3.4-1968\r\n" +
+ "Content-Transfer-Encoding: binary\r\n" +
+ "\r\n" +
+ "some random whatever\r\n" +
+ "--foo\r\n" +
+ "Content-Disposition: form-data; name=\"field3\"; " +
"filename=\"file.tmp\"\r\n" +
+ "Content-Type: application/octet-stream\r\n" +
+ "Content-Transfer-Encoding: binary\r\n" +
"\r\n" +
"some random whatever\r\n" +
"--foo--\r\n";
String s = out.toString("US-ASCII");
assertEquals(expected, s);
assertEquals(-1, multipart.getTotalLength());
-
+
tmpfile.delete();
}