|
| 1 | +package org.baeldung.httpclient; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.File; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStreamReader; |
| 7 | + |
| 8 | +import org.apache.http.HttpEntity; |
| 9 | +import org.apache.http.HttpResponse; |
| 10 | +import org.apache.http.client.HttpClient; |
| 11 | +import org.apache.http.client.fluent.Request; |
| 12 | +import org.apache.http.client.fluent.Response; |
| 13 | +import org.apache.http.client.methods.HttpPost; |
| 14 | +import org.apache.http.entity.ContentType; |
| 15 | +import org.apache.http.entity.mime.HttpMultipartMode; |
| 16 | +import org.apache.http.entity.mime.MultipartEntityBuilder; |
| 17 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 18 | +import org.junit.Before; |
| 19 | +import org.junit.BeforeClass; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +public class HttpClientMultipartTest { |
| 23 | + |
| 24 | + private static final String SERVER = "http://cgi-lib.berkeley.edu/ex/fup.cgi"; |
| 25 | + private static final String SERVER2 = "http://posttestserver.com/post.php"; |
| 26 | + private static final String SERVER3 = "http://postcatcher.in/catchers/53765b0349c306020000077b"; |
| 27 | + private static final String SERVER4 = "http://echo.200please.com"; |
| 28 | + private static final String SERVER5 = "http://greensuisse.zzl.org/product/dump/dump.php"; |
| 29 | + private static final String SERVER6 = "http://www.newburghschools.org/testfolder/dump.php"; |
| 30 | + private static HttpClient client; |
| 31 | + private static HttpPost post; |
| 32 | + private static String textFileName; |
| 33 | + private static String imageFileName; |
| 34 | + private static String zipFileName; |
| 35 | + |
| 36 | + @BeforeClass |
| 37 | + public static void setUpBeforeClass() throws Exception { |
| 38 | + client = HttpClientBuilder.create().build(); |
| 39 | + post = new HttpPost(SERVER2 /*"fup.cgi"*/); |
| 40 | + textFileName = ".\temp.txt"; |
| 41 | + imageFileName = "image.jpg"; |
| 42 | + zipFileName = "zipFile.zip"; |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + @Before |
| 47 | + public void setUp() throws Exception { |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + /* @Test |
| 52 | + public final void whenUploadWithAddPart_thenNoExceptions() throws IOException { |
| 53 | +
|
| 54 | + |
| 55 | + final File file = new File(textFileName); |
| 56 | + final FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY); |
| 57 | + final StringBody stringBody1 = new StringBody("This is message 1", ContentType.MULTIPART_FORM_DATA); |
| 58 | + final StringBody stringBody2 = new StringBody("This is message 2", ContentType.MULTIPART_FORM_DATA); |
| 59 | + final MultipartEntityBuilder builder = MultipartEntityBuilder.create(); |
| 60 | + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); |
| 61 | + builder.addPart("submitted", fileBody); |
| 62 | + builder.addPart("note", stringBody1); |
| 63 | + builder.addPart("note2", stringBody2); |
| 64 | + final HttpEntity entity = builder.build(); |
| 65 | + post.setEntity(entity); |
| 66 | + final HttpResponse response = client.execute(post); |
| 67 | + System.out.println(getContent(response)); |
| 68 | + Header[] headers = response.getAllHeaders(); |
| 69 | +
|
| 70 | + for (Header thisHeader : headers) { |
| 71 | + System.out.println(thisHeader.getName() + ":" + thisHeader.getValue()); |
| 72 | + } |
| 73 | + } */ |
| 74 | + /*@Test |
| 75 | + public final void whenUploadWithAddBinaryBodyandAddTextBody_ThenNoExeption() throws ClientProtocolException, IOException { |
| 76 | + |
| 77 | + final File file = new File(textFileName); |
| 78 | + String message = "This is a multipart post"; |
| 79 | + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); |
| 80 | + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); |
| 81 | + builder.addBinaryBody("submitted", file, ContentType.DEFAULT_BINARY, textFileName); |
| 82 | + builder.addTextBody("note", message, ContentType.TEXT_PLAIN); |
| 83 | + final HttpEntity entity = builder.build(); |
| 84 | + post.setEntity(entity); |
| 85 | + HttpResponse response = client.execute(post); |
| 86 | + System.out.println(getContent(response)); |
| 87 | + Header[] headers = response.getAllHeaders(); |
| 88 | +
|
| 89 | + for (Header thisHeader : headers) { |
| 90 | + System.out.println(thisHeader.getName() + ":" + thisHeader.getValue()); |
| 91 | + } |
| 92 | +
|
| 93 | + }*/ |
| 94 | + |
| 95 | + /* @Test |
| 96 | + public final void whenUploadWithAddBinaryBody_NoType_andAddTextBody_ThenNoExeption() throws ClientProtocolException, IOException { |
| 97 | +
|
| 98 | + final File file = new File(imageFileName); |
| 99 | + final String message = "This is a multipart post"; |
| 100 | + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); |
| 101 | + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); |
| 102 | + builder.addBinaryBody("submitted", file, ContentType.DEFAULT_BINARY, textFileName); |
| 103 | + //builder.addBinaryBody("upfile", fileBin); |
| 104 | + builder.addTextBody("note", message, ContentType.TEXT_PLAIN); |
| 105 | + final HttpEntity entity = builder.build(); |
| 106 | + post.setEntity(entity); |
| 107 | + final HttpResponse response = client.execute(post); |
| 108 | + System.out.println(getContent(response)); |
| 109 | + Header[] headers = response.getAllHeaders(); |
| 110 | +
|
| 111 | + for (Header thisHeader : headers) { |
| 112 | + System.out.println(thisHeader.getName() + ":" + thisHeader.getValue()); |
| 113 | + } |
| 114 | +
|
| 115 | + }*/ |
| 116 | + |
| 117 | + /* @Test |
| 118 | + public final void whenUploadWithAddBinaryBody_InputStream_andTextBody_ThenNoException() throws ClientProtocolException, IOException{ |
| 119 | + final InputStream inputStream = new FileInputStream(zipFileName); |
| 120 | + final String message = "This is a multipart post"; |
| 121 | + final MultipartEntityBuilder builder = MultipartEntityBuilder.create(); |
| 122 | + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); |
| 123 | + // builder.addBinaryBody("submitted", inputStream, ContentType.create("application/zip"), "zipFileName"); |
| 124 | + builder.addBinaryBody("upfile", inputStream, ContentType.create("application/zip"), "zipFileName"); |
| 125 | + builder.addTextBody("note", message, ContentType.TEXT_PLAIN); |
| 126 | + final HttpEntity entity = builder.build(); |
| 127 | + post.setEntity(entity); |
| 128 | + final HttpResponse response = client.execute(post); |
| 129 | + |
| 130 | + System.out.println(getContent(response)); |
| 131 | + Header[] headers = response.getAllHeaders(); |
| 132 | +
|
| 133 | + for (Header thisHeader : headers) { |
| 134 | + System.out.println(thisHeader.getName() + ":" + thisHeader.getValue()); |
| 135 | + } |
| 136 | +
|
| 137 | + |
| 138 | + }*/ |
| 139 | + |
| 140 | + // BUG |
| 141 | + @Test |
| 142 | + public final void whenFluentRequestWithBody_ThenNoException() throws IOException{ |
| 143 | + |
| 144 | + final String fileName = ".\temp.txt"; |
| 145 | + final File fileBin = new File(fileName); |
| 146 | + final String message = "This is a multipart post"; |
| 147 | + final MultipartEntityBuilder builder = MultipartEntityBuilder.create(); |
| 148 | + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); |
| 149 | + builder.addBinaryBody("upfile", fileBin, ContentType.DEFAULT_BINARY, fileName); |
| 150 | + builder.addTextBody("note", message, ContentType.TEXT_PLAIN); |
| 151 | + final HttpEntity entity = builder.build(); |
| 152 | + final Response response = Request.Post(SERVER) |
| 153 | + .body(entity).execute(); |
| 154 | + } |
| 155 | + |
| 156 | + public static String getContent(final HttpResponse response) throws IOException { |
| 157 | + |
| 158 | + final BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); |
| 159 | + String body = ""; |
| 160 | + String content = ""; |
| 161 | + |
| 162 | + while ((body = rd.readLine()) != null) { |
| 163 | + content += body + "\n"; |
| 164 | + } |
| 165 | + return content.trim(); |
| 166 | + } |
| 167 | + |
| 168 | + |
| 169 | +} |
0 commit comments