use of org.apache.http.entity.mime.content.FileBody in project undertow by undertow-io.
the class MultipartFormDataParserTestCase method testFileUploadWithSmallFileSizeThreshold.
@Test
public void testFileUploadWithSmallFileSizeThreshold() throws Exception {
DefaultServer.setRootHandler(new BlockingHandler(createInMemoryReadingHandler(10)));
TestHttpClient client = new TestHttpClient();
try {
HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8));
entity.addPart("file", new FileBody(new File(MultipartFormDataParserTestCase.class.getResource("uploadfile.txt").getFile())));
post.setEntity(entity);
HttpResponse result = client.execute(post);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
String resp = HttpClientUtils.readResponse(result);
Map<String, String> parsedResponse = parse(resp);
Assert.assertEquals("false", parsedResponse.get("in_memory"));
Assert.assertEquals("uploadfile.txt", parsedResponse.get("file_name"));
Assert.assertEquals(DigestUtils.md5Hex(new FileInputStream(new File(MultipartFormDataParserTestCase.class.getResource("uploadfile.txt").getFile()))), parsedResponse.get("hash"));
} finally {
client.getConnectionManager().shutdown();
}
}
use of org.apache.http.entity.mime.content.FileBody in project heron by twitter.
the class HttpUploader method uploadPackageAndGetURI.
private URI uploadPackageAndGetURI(final CloseableHttpClient httpclient) throws IOException, URISyntaxException {
File file = new File(this.topologyPackageLocation);
String uploaderUri = HttpUploaderContext.getHeronUploaderHttpUri(this.config);
post = new HttpPost(uploaderUri);
FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart(FILE, fileBody);
HttpEntity entity = builder.build();
post.setEntity(entity);
HttpResponse response = execute(httpclient);
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8.name());
LOG.fine("Topology package download URI: " + responseString);
return new URI(responseString);
}
use of org.apache.http.entity.mime.content.FileBody in project tutorials by eugenp.
the class HttpClientMultipartLiveTest method givenFileandMultipleTextParts_whenUploadwithAddPart_thenNoExceptions.
// tests
@Test
public final void givenFileandMultipleTextParts_whenUploadwithAddPart_thenNoExceptions() throws IOException {
final URL url = Thread.currentThread().getContextClassLoader().getResource("uploads/" + TEXTFILENAME);
final File file = new File(url.getPath());
final FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
final StringBody stringBody1 = new StringBody("This is message 1", ContentType.MULTIPART_FORM_DATA);
final StringBody stringBody2 = new StringBody("This is message 2", ContentType.MULTIPART_FORM_DATA);
//
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("upfile", fileBody);
builder.addPart("text1", stringBody1);
builder.addPart("text2", stringBody2);
final HttpEntity entity = builder.build();
//
post.setEntity(entity);
response = client.execute(post);
final int statusCode = response.getStatusLine().getStatusCode();
final String responseString = getContent();
final String contentTypeInHeader = getContentTypeHeader();
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
// assertTrue(responseString.contains("Content-Type: multipart/form-data;"));
assertTrue(contentTypeInHeader.contains("Content-Type: multipart/form-data;"));
System.out.println(responseString);
System.out.println("POST Content Type: " + contentTypeInHeader);
}
use of org.apache.http.entity.mime.content.FileBody in project questdb by bluestreak01.
the class HttpTestUtils method upload.
public static int upload(String resource, String url, String schema, StringBuilder response) throws IOException {
HttpPost post = new HttpPost(url);
try (CloseableHttpClient client = HttpClients.createDefault()) {
MultipartEntityBuilder b = MultipartEntityBuilder.create();
if (schema != null) {
b.addPart("schema", new StringBody(schema, ContentType.TEXT_PLAIN));
}
b.addPart("data", new FileBody(resourceFile(resource)));
post.setEntity(b.build());
HttpResponse r = client.execute(post);
if (response != null) {
InputStream is = r.getEntity().getContent();
int n;
while ((n = is.read()) > 0) {
response.append((char) n);
}
is.close();
}
return r.getStatusLine().getStatusCode();
}
}
use of org.apache.http.entity.mime.content.FileBody in project selenium_java by sergueik.
the class RestClient method request.
private JSON request(HttpEntityEnclosingRequestBase req, Issue.NewAttachment... attachments) throws RestException, IOException {
if (attachments != null) {
req.setHeader("X-Atlassian-Token", "nocheck");
MultipartEntity ent = new MultipartEntity();
for (Issue.NewAttachment attachment : attachments) {
String filename = attachment.getFilename();
Object content = attachment.getContent();
if (content instanceof byte[]) {
ent.addPart("file", new ByteArrayBody((byte[]) content, filename));
} else if (content instanceof InputStream) {
ent.addPart("file", new InputStreamBody((InputStream) content, filename));
} else if (content instanceof File) {
ent.addPart("file", new FileBody((File) content, filename));
} else if (content == null) {
throw new IllegalArgumentException("Missing content for the file " + filename);
} else {
throw new IllegalArgumentException("Expected file type byte[], java.io.InputStream or java.io.File but provided " + content.getClass().getName() + " for the file " + filename);
}
}
req.setEntity(ent);
}
return request(req);
}
Aggregations