Return-Path: Delivered-To: apmail-hc-commits-archive@www.apache.org Received: (qmail 42114 invoked from network); 3 Sep 2009 21:15:46 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 3 Sep 2009 21:15:46 -0000 Received: (qmail 40015 invoked by uid 500); 3 Sep 2009 21:15:46 -0000 Delivered-To: apmail-hc-commits-archive@hc.apache.org Received: (qmail 39985 invoked by uid 500); 3 Sep 2009 21:15:46 -0000 Mailing-List: contact commits-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpComponents Project" Delivered-To: mailing list commits@hc.apache.org Received: (qmail 39976 invoked by uid 99); 3 Sep 2009 21:15:46 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 03 Sep 2009 21:15:46 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 03 Sep 2009 21:15:37 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id C81C223888E7; Thu, 3 Sep 2009 21:15:17 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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 Date: Thu, 03 Sep 2009 21:15:17 -0000 To: commits@hc.apache.org From: olegk@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090903211517.C81C223888E7@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 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(); }